1001. A+B Format (20)
2016-04-05 08:42
239 查看
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 9
Sample 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; } }
相关文章推荐
- ZOJ3228 Searching the String(AC自动机)
- java带图片的邮件发送方法实现
- Swift基础知识
- Tomcat 在IE中下载rar文件直接以乱码方式打开解决方案
- 数据库系统概念学习03-体系结构
- 关于Asp.net中Page.IsCrossPagePostBack的使用
- 我的linux学习之路:apt-get常用命令
- 第五周学习进度
- JavaScript事件冒泡简介及应用
- iOS开发之cocoapods的安装与使用
- js之操作JSON数据
- 关联库文件出现的2个错误
- (转)成功男人早晨有五个习惯
- 蓝桥杯b组c++.2
- 使用系统默认接口来剪切图片
- C#16位MD5加密算法
- C#流(stream)
- 雅虎股票接口 转载
- LeetCode 114. Flatten Binary Tree to Linked List
- 详解Javascript中的Array对象