PAT A 1001. A+B Format (20)

来源:互联网 发布:linux shell 等待 编辑:程序博客网 时间:2024/04/30 04:54

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 <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;}
// 结果


0 0
原创粉丝点击