1001. A+B Format (20)

字符串模拟数字操作

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

int main()
{
	int a, b;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		int ans = a+b;
		int flag = 1;//1 nonnegtive; 0 negtive
		if(ans < 0) flag = 0, ans = -ans;
		//get output string
		std::string output;
		int cnt = 0;//count for ','
		do
		{
			int temp = ans%10;
			if(cnt > 0 && cnt%3 == 0) 
				output.insert(output.begin(), ',');
			output.insert(output.begin(), temp+'0');
			ans/=10;
			cnt++;
		}while(ans != 0);
		//for negtive case
		if(flag == 0) output.insert(output.begin(), '-');
		//output
		printf("%s\n",output.c_str());
	}
	return 0;
}


 

 

你可能感兴趣的:(pat,ZJU)