码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2562 Primary Arithmetic(简单题)

时间:2014-09-23 22:46:05      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:style   io   os   ar   sp   on   c   代码   amp   

【题意简述】:计算两数相加,有多少个进位。

【分析】:很简单,不过还是要注意输出的细节。当进位为1时,输出的operation,没有s。


详见代码:

// 216K 0Ms
#include<iostream>
using namespace std;

int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		if(a == 0&&b == 0) break; // 在这贡献了n次WA~,傻!
		int ans = 0;
		int tmp1,tmp2;
		int sum = 0;
		while(1)
		{
			tmp1 = a%10;
			a /= 10;
			
			tmp2 = b%10;
			b /= 10;
			
			sum = tmp1 + tmp2 + sum;
			if(sum>=10)
			{
				ans++;
				sum = 1;
			}
			else
				sum = 0;
			if(a == 0&&b == 0) break;
		}
		if(ans == 0)
			cout<<"No carry operation."<<endl;
		else if(ans == 1)
			cout<<"1 carry operation."<<endl;
		else
			cout<<ans<<" carry operations."	<<endl;
	}
	return 0;
}


POJ 2562 Primary Arithmetic(简单题)

标签:style   io   os   ar   sp   on   c   代码   amp   

原文地址:http://blog.csdn.net/u013749862/article/details/39502939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!