1.原题
1001 A+B Format
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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
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
2.源代码
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> m;
int a,b,sum;
cin>>a>>b;
sum=a+b;
if(a+b<0)
{
cout<<'-';
sum=-sum;
}
if(sum==0)m.push(0);
while(sum!=0)
{
m.push(sum%10);
sum/=10;
}
int sna=1;
switch(m.size()%3)
{
case 1:sna=3;break;
case 2:sna=2;break;
}
while(!m.empty())
{
cout<<m.top();
m.pop();
sna++;
if(sna==4)
{
if(!m.empty())cout<<",";
sna=1;
}
}
return 0;
}
第一次写博客,来个题熟悉下操作。