问题描述:计算两个范围为[]数的和,并按照xx,xxx,xxx的格式输出。
解题思路:32位的int的值大概为左右,所以直接相加,再递归或迭代三位数一组输出。
AC代码:
/*
**1001 A+B Format (20 分)
**整形范围内的数相加,按格式输出
**
*/
#include<iostream>
#include<stack>
using namespace std;
stack<int>out;
#define Format 1000
int main()
{
int a,b,c;
//freopen("test.txt","r",stdin);
scanf("%d %d",&a,&b);
c=a+b;
if(c<0){c=-c;printf("-");}
do{
out.push(c%Format);
c/=Format;
}while(c);
printf("%d",out.top());
out.pop();
while(out.size()){
printf(",%03d",out.top());
out.pop();
}
return 0;
}