1001 A+B Format (20 分)
阿新 • • 發佈:2021-06-18
Calculatea+band 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 integersaandbwhere−. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum ofaandbin one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
思路:
對兩個數的和c進行討論,先將c對映到正區間,對1000迴圈取餘存入陣列,然後倒序輸出即可,注意需要填0補位,另外第一個數不能填0補位
#include<bits/stdc++.h> using namespacestd; const int maxn=10010; int main(){ int a,b; scanf("%d %d",&a,&b); int c=a+b; if(c<0){ printf("-"); c=-c; } int num[maxn]; int i=0; if(c==0){ printf("%d\n",0); return 0; } while(c>0){ num[i]=c%1000; c/=1000; i++; } printf("%d",num[i-1]); for(int j=i-2;j>=0;j--){ printf(",%03d",num[j]); } printf("\n"); return 0; }