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 −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); }
hello my world
本文来自博客园,作者:slowlydance2me,转载请注明原文链接:https://www.cnblogs.com/slowlydance2me/p/slowlydance2me_1001A.html