1001. A+B Format (20)
来源:互联网 发布:电脑控制手机源码 编辑:程序博客网 时间:2024/06/11 22:03
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,99
方法一:#include <iostream>#include <string>using namespace std;int n[7];char c[10];string intConvertString(int a) { string str; int i, j = 0, k = 0; int temp = 0; for(i=0; i<7; i++) { n[i] = a%10; a /=10; if(n[i]!=0) { temp = i + 1; } } for(i=0; i<temp; i++) { c[j++] = n[i]+'0'; if((i+1)%3 == 0&&i+1!=temp) { c[j++] = ','; } } for(i=0; i<j/2; i++) { temp = c[i]; c[i] = c[j-i-1]; c[j-i-1] = temp; } return c;}int main() { int a, b, sum; while(cin>>a>>b) { sum = a + b; if(sum > 0) { cout<<intConvertString(sum)<<endl; } else if(sum < 0){ cout<<"-"<<intConvertString(-sum)<<endl; } else { cout<<"0"<<endl; } } return 0;}方法二:#include <iostream>#include <string>#include <sstream>using namespace std;int main(){ int a,b; cin>>a>>b; int c = a + b; stringstream strStream; strStream<<c; //int转string的方法! string s = strStream.str(); for(int i = s.size() - 3; i > 0 && s[i - 1] != '-'; i -= 3) s.insert(i, ","); cout<<s<<endl;}
0 0
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- Objective-C 入门初级学习
- 社説 20150223 保釈率の上昇 「人質司法」見直しを進めたい
- 社説 20150223 将来の電源構成 原発の適切な活用が現実的だ
- 基于空间网格的点云处理备案记录
- Android 采用PULL来解析XML
- 1001. A+B Format (20)
- win32项目与win32控制台应用程序的转换方法
- Java线程池使用说明
- Java的几种访问权限
- poj 2392 多重背包
- 1002. A+B for Polynomials (25)
- ASP.NET一款免费富文本(RichText)编辑器KindEditor,实现在线排版编辑文章。这里简单介绍下如何使用
- 1003. Emergency (25)
- 1004. Counting Leaves (30)