首页 > 代码库 > POJ 2562 Primary Arithmetic(简单题)

POJ 2562 Primary Arithmetic(简单题)

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

【分析】:很简单,不过还是要注意输出的细节。当进位为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(简单题)