mirror of
https://github.com/xlucn/PAT.git
synced 2026-09-23 20:47:57 +08:00
change to another method using string manipulation with better logic and readability
This commit is contained in:
+10
-10
@@ -23,22 +23,22 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a, b, sum, count, dec;
|
||||
int a, b, pos;
|
||||
char num[11];
|
||||
|
||||
scanf("%d%d", &a, &b);
|
||||
sum = a + b;
|
||||
sprintf(num, "%d", a + b);
|
||||
|
||||
if(sum == 0) putchar('0');
|
||||
if(sum < 0) { putchar('-'); sum = -sum; }
|
||||
|
||||
for(count = 0, dec = 1; sum >= dec; count++, dec *= 10) ;
|
||||
|
||||
for(int c = count; c; c--, sum %= dec)
|
||||
for(pos = strlen(num) - 3; pos > 0 && num[pos - 1] != '-'; pos -= 3)
|
||||
{
|
||||
if(c < count && c % 3 == 0 && c) putchar(',');
|
||||
putchar('0' + sum / (dec /= 10));
|
||||
memmove(num + pos + 1, num + pos, strlen(num) - pos + 1);
|
||||
num[pos] = ',';
|
||||
}
|
||||
|
||||
puts(num);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user