AcWing 1473. A + B 格式
原题链接
简单
作者:
_如鲸向海
,
2022-06-26 12:03:33
,
所有人可见
,
阅读 149
C++ 代码(甲级01day)
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
a = a+b;
char str[12];
int flag = 0;
if(a<0) flag = 1;
string s = to_string(abs(a));
reverse(s.begin(), s.end());
int j = 0;
for(int i = 0,count = 1;i<s.length();i++){
str[j++] = s[i];
if(count%3==0&&i!=s.length()-1){
str[j++] = ',';
}
count++;
}
if(flag) str[j++] = '-';
reverse(str,str+j);
str[j] = '\0';
cout<<str<<endl;
}