1001 A+B Format (20)(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
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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
注意几个输出点:-991 991 1001 -1001
1 #include<iostream> 2 #include <sstream> 3 #include<string> 4 #include<stack> 5 #include<typeinfo> 6 7 using namespace std; 8 9 int main() 10 { 11 int a,b,flag=0,temp=0; 12 13 cin>>a>>b; 14 15 a+=b; 16 17 if(a<0) 18 { 19 flag=1; 20 cout<<"-"; 21 } 22 23 stringstream ss; 24 ss<<a; 25 26 string str=ss.str(); 27 28 stack<char> s; 29 30 for(int i=str.length()-1;i>=flag;--i) 31 { 32 s.push(str[i]); 33 ++temp; 34 35 if(temp%3==0 && i-1>=flag) 36 s.push(‘,‘); 37 } 38 39 while(!s.empty()) 40 { 41 cout<<s.top(); 42 s.pop(); 43 } 44 45 cout<<endl; 46 47 return 0; 48 }
原文地址:https://www.cnblogs.com/cdp1591652208/p/9354123.html
时间: 2024-10-10 07:59:02