1. 程式人生 > >1001 A+B Format (20 分)

1001 A+B Format (20 分)

reverse integer uno calculate sum span tst all ins

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 ?10?^6??a,b10^?6??. 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


分析:註意到a,b的範圍可以用int型表示,int可以表示10^9的範圍,一開始想復雜了,其實直接把字符串逆置後,從高位輸出,每輸出3個數字輸出一個逗號就行了。
 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
4 * Author : Mered1th 5 * Date : 2019-02-24-19.21.16 6 * Description : A1001 7 */ 8 #include<cstdio> 9 #include<cstring> 10 #include<iostream> 11 #include<cmath> 12 #include<algorithm> 13 #include<string> 14 #include<unordered_set> 15 #include<map> 16 #include<vector> 17
#include<set> 18 using namespace std; 19 20 int main(){ 21 #ifdef ONLINE_JUDGE 22 #else 23 freopen("1.txt", "r", stdin); 24 #endif 25 int a,b,c; 26 scanf("%d%d",&a,&b); 27 c=a+b; 28 if(c<0){ 29 printf("-"); 30 c=-c; 31 } 32 string s=to_string(c); 33 reverse(s.begin(),s.end()); 34 int len=s.length(); 35 for(int i=len-1;i>=0;i--){ 36 if((i+1)%3==0&&i!=len-1&&i>0){ 37 printf(","); 38 } 39 printf("%c",s[i]); 40 } 41 return 0; 42 }

 

1001 A+B Format (20 分)