现在的位置: 首页 > 综合 > 正文

【PAT Advanced Level】1001. A+B Format (20)

2017年12月15日 ⁄ 综合 ⁄ 共 294字 ⁄ 字号 评论关闭

一开始用了locale,但是提交显示段错误,然后就用了传统方法,转化为字符串处理。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	
	int a,b;
	cin>>a>>b;
	int c = a + b;
	stringstream strStream;  
	strStream<<c;			//int转string的方法!
	string s = strStream.str();

	for(int i = s.size() - 3; i > 0 && s[i - 1] != '-'; i -= 3)
		s.insert(i, ",");

	cout<<s<<endl;
}

抱歉!评论已关闭.