1001. A+B Format (20)
来源:互联网 发布:猴王水果竞猜网站源码 编辑:程序博客网 时间:2024/06/15 08:35
1001. A+B Format (20)
时间限制
400 ms
内存限制
65536 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.
Sample Input-1000000 9Sample Output
-999,991
//需要考虑完整的情况,比如和为零的情况,三位数以及以下的情况,还有一个特殊情况即六位数的情况(这个时候可以直接将逗号插入),四位数以及五位数七位数的情况可以放在一块考虑#include<iostream>#include<vector>using namespace std;int main(){long long int a=0,b=0,c=0;int temp=0,num=0,flag=0;vector<char> vec1;vector<char>::iterator iter;cin>>a>>b;c=a+b;if(c<0)//标志位flag,当为负数的时候,标志位置1{flag=1;c=c*(-1);}if(c>=1000)//大于四位数时候{if(c>=100000&&c<=999999)//特殊考虑六位数,直接将逗号插入{while(c){temp=c%10;vec1.push_back(temp+48);c=c/10;}vec1.insert(vec1.begin()+3,',');}else//其他情况按照三个一组插入逗号,其实也可以不分开考虑,直接考虑vector最后一个元素是否为‘,’如果是逗号,弹出{while(c){temp=c%10;vec1.push_back(temp+48);num++;c=c/10;if(num==3){vec1.push_back(',');num=0;}}}if(flag==1)cout<<'-';for(int i=vec1.size()-1;i>=0;i--)cout<<vec1.at(i);}else//三位数以下时候直接输出,注意为负数时候要输出负号{if(flag==1)cout<<'-';cout<<c;}}
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)
- 关联库文件出现的2个错误
- 我的linux学习之路:apt-get常用命令
- JavaScript事件冒泡简介及应用
- 数据库系统概念学习03-体系结构
- 关于Asp.net中Page.IsCrossPagePostBack的使用
- 1001. A+B Format (20)
- Javascript引用类型学习
- Tomcat 在IE中下载rar文件直接以乱码方式打开解决方案
- 5种常用的四轴飞行器PID算法讲解集合
- Genymotion 安卓模拟器 unable to start the virtual device.
- 策略模式
- Eclipse下的java工程目录
- 预处理器和宏
- 设置MyEclipse黑色主题背景