PAT (Advanced Level) Practice 1001 A+B Format 分数 20

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 106a,b106. 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.

Sample Input:

-1000000 9
 

Sample Output:

-999,991
 
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
 

解题:

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

int main()
{
int a=0,b=0,sum=0;
scanf("%d %d",&a,&b);
sum=a+b;

if(sum/1000000)
{
printf("%d",sum/1000000);
printf(",");
printf("%03d",abs(sum%1000000/1000));
printf(",");
printf("%03d",abs(sum%1000000%1000));
}
if(sum/1000000==0&&sum/1000)
{
printf("%d",sum/1000);
printf(",");
printf("%03d",abs(sum%1000));
}
if(sum/1000000==0&&sum/1000==0)
printf("%d",sum);

}

 

posted @ 2022-09-22 15:11  slowlydance2me  阅读(22)  评论(0编辑  收藏  举报