Posted by MichaelChen on
2021-01-24
Estimated Reading Time 1 Minutes
Words 293 In Total
Viewed Times
A+B Format
Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −10^6<=a,b<=10^6. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
intmain(){ int a = 0; int b = 0; while (scanf("%d %d",&a, &b) != EOF){ int c = 0; char* cStr = ""; c = a + b; int count = 0; char fu = ','; cStr = malloc(1000); sprintf(cStr,"%d",c); int len = strlen(cStr); int number = 0; if (c < 0){ if ((len - 1) % 3 == 0){ number = (len - 1) / 3 - 1; } else{ number = (len - 1) / 3; } } else{ if (len % 3 == 0){ number = len / 3 - 1; } else{ number = len / 3; } }
char *ans; int pos = len + number - 1; ans = malloc(len + number + 1); for (int i = len - 1; i >= 0; i--) { char cut = cStr[i]; if (cut == '-'){ count = 0; count += 1; ans[pos] = cut; pos -= 1; continue; } if (count == 3){ ans[pos] = (char) fu; count = 0; pos -= 1; } count += 1; ans[pos] = cut; pos -= 1; } for (int j = 0; j < len + number; j++) { printf("%c",ans[j]); } printf("\n"); }
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !