PAT (Advanced Level) Practise:1001. A+B Format

时间:2023-03-09 07:42:38
PAT (Advanced Level) Practise:1001. 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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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.

Sample Input

-1000000 9

Sample Output

-999,991

题意分析:

输出(a+b)的结果,输出格式需要使用逗号把数值分割。

提交代码:

 #include <stdio.h>

 int main(void)
{
int out[];
int i, len, tmp;
int a, b, c; scanf("%d %d", &a, &b); c = a + b;
if(c < )
{
printf("-");
c = -c;
} len = ;
do {
out[len] = c % ;
c /= ;
len++;
} while(c != ); for(i = ; i < len; i++)
{
printf("%d", out[len--i]);
tmp = len - i - ;
//if((len-i-1)%3 == 0 && (len-i) > 1)
if(tmp != && tmp % == )
printf(",");
} return ;
}