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

2022-11-04,,,,

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 −106≤a,b≤106. 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); }

PAT (Advanced Level) Practice 1001 A+B Format 分数 20的相关教程结束。

《PAT (Advanced Level) Practice 1001 A+B Format 分数 20.doc》

下载本文的Word格式文档,以方便收藏与打印。