首页 > 代码库 > 1001. A+B Format (20)
1001. A+B Format (20)
时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.
1 #include <iostream> 2 #include <string> 3 #include <sstream> 4 using namespace std; 5 int main() 6 { 7 int n1,n2,sum; 8 9 while(cin>>n1)10 {11 cin>>n2;12 sum=n1+n2;13 stringstream ss; string s;14 ss<<sum;15 ss>>s;16 int i;17 char ch[20];18 int len=0;int num=0;19 20 for(i=s.length()-1;i>=0;i--)21 {22 ch[len++]=s[i];23 num++;24 if(sum>=0&&num==3&&i!=0) 25 {26 ch[len++]=‘,‘;27 num=0;28 }29 if(sum<0&&num==3&&i!=0&&i!=1) 30 {31 ch[len++]=‘,‘;32 num=0;33 }34 }35 36 37 for(i=len-1;i>=0;i--)38 cout<<ch[i];39 40 cout<<endl;41 42 }43 return 0;44 }
1001. A+B Format (20)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。