PAT A 1001. A+B Format (20)
2017-02-20 01:32
519 查看
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
Sample Output
-999,991
// 题目分析:
没有难点,可以有多种实现方法,此处练习堆栈操作
// 代码
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 <string> #include <stack> using namespace std; int main(){ int a,b; cin>>a>>b; int c=a+b; if(c<0){ c=-c; cout<<'-'; }else if(c==0){ cout<<0; return 0; } stack<char> s; int count=0;//计数器 while(c!=0){ if(count%3==0&&count!=0){ s.push(','); } s.push(c%10+'0'); count++; c/=10; } while(!s.empty()){ cout<<s.top(); s.pop(); } system("pause"); return 0; }
// 结果
相关文章推荐
- PAT A1001 A+B Format(20)
- PAT A 1001. A+B Format (20)
- 1001. A+B Format (20)
- (PAT-A)1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT甲级1001. A+B Format (20)
- PAT 1001. A+B Format (20)
- PAT A 1001 A+B Format
- 1001. A+B Format (20)
- PAT - 甲级 - 1001. A+B Format (20)(字符串处理)
- 关于‘1001.A+B Format (20)’的解题报告
- PAT---A1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT: 1001. A+B Format (20)
- 1001. A+B Format (20)
- PAT甲级1001. A+B Format (20)
- PAT 1001. A+B Format (20) c++版
- 1001. A+B Format (20)
- 【PAT甲级】【C】1001. A+B Format (20)