从个位开始3个3个摘取相加的结果 和 逗号 压如栈中,再从栈中输出即可。
题目链接:https://www.patest.cn/contests/pat-a-practise/1001
#include <iostream>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
using namespace std;
int main(int argc, char** argv) {
int a,b;
cin >> a >> b;
int c = a+b;
stack<char> s;
if(c<0)
cout<<"-";
c = abs(c);
while(1)
{
int d = c%1000;
if(c/1000 > 0)
{
s.push(d%10+'0');
s.push(d/10%10+'0');
s.push(d/100+'0');
s.push(',');
c = c/1000;
}
else
{
cout << c;
break;
}
}
while(!s.empty())
{
cout << s.top();
s.pop();
}
return 0;
}