您的位置:首页 > 其它

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

2016-07-31 23:23 459 查看
A+B Format (20)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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

思路:一开始想的比较复杂,后面发现这题可以取巧,利用格式输出,取出相应位置上的数值。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
int a, b, i, j , n;
char s[1024] = {0};
int sum = 0;
scanf("%d %d", &a, &b);
sum = a + b;

if(sum < 0) //是负数
{
printf("-");
sum = -sum;
}
//分三种情况讨论
if(sum < 1000)
printf("%d\n", sum);
else if(sum >= 1000000)
{
printf("%d,%03d,%03d\n", sum/1000000, (sum/1000)%1000, sum%1000);
}
else
{
printf("%d,%03d\n", sum/1000, sum%1000);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT