PAT (Advanced Level) Practice 1001 A+B Format (20分)
题目描述:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400
考点:
1.stringstream 可实现 int & string 间的类型转换。
int c = a+b;
stringstream ss;
ss<<c;
string c_str = ss.str();
int d;
ss>>d;
2.string.insert(index, “obj”)
3.string.substr(start, length)
代码:
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main(){
int a,b;cin>>a>>b;
int c = a+b;
stringstream ss;
ss<<c;
string c_str = ss.str();
if(c<0){
c_str = c_str.substr(1);
}
int k = c_str.length()-1-2;
while(k>0){
c_str.insert(k,",");
k = k-1-2;
}
if(c<0){
c_str = "-"+c_str;
}
cout<<c_str<<endl;
}