知识点
- int转string
stringstream ss; string s = ss.str();
- 字符串反转
reverse(s.begin(),s.end())
代码
#include <algorithm>
#include<cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char* argv[])
{
int a,b;
cin>>a>>b;
bool isNegative=false;//是否是负数
int c=a+b;
stringstream ss;//将int转换为string
if(c<0)
{
isNegative=true;
c=-c;
}
else if(c==0)
{
cout<<c;
return 0;
}
int num=0;
while(c>0){
int d=c%10;
c=c/10;
ss<<d;;
num++;
if(num==3&&c!=0)
{
ss<<",";
num=0;
}
}
string s = ss.str();
reverse(s.begin(),s.end());//字符串翻转
if( isNegative)
cout<<"-";
cout<< s <<endl;
return 0;
}