poj2562 Primary Arithmetic

Description

Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.

Input

Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0.

Output

For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below.

Sample Input

123 456
555 555
123 594
0 0

Sample Output

No carry operation.
3 carry operations.
1 carry operation.

AC Code

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <queue>
#define inf 0x3f3f3f3f
#define MAXM 200005
using namespace std;
const double pi=3.141592653589793239,e=2.7182818284590452354;



int main(int argc, char** argv) {
	char p[100],q[100];
	int a[100],b[100];
	while(cin>>p>>q)
	{
		int sum=0,i,j,m,c=0;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		if(p[0]=='0'&&q[0]=='0')
			break;
		m=strlen(p);
		for(i=0;i<m;i++)
		{
			a[m-i-1]=p[i]-'0';
		}
		m=strlen(q);
		for(i=0;i<m;i++)
		{
			b[m-i-1]=q[i]-'0';
		}
		m=max(strlen(p),strlen(q));
		for(i=0;i<m;i++)
		{
			int tmp;
			tmp=a[i]+b[i]+c;
			if(tmp>9)
			{
				sum++;
				c=tmp-9;
			}
			else
				c=0;
		}
		if(sum>1)
			cout<<sum<<" carry operations."<<endl;
		else if(sum==1)
			cout<<sum<<" carry operation."<<endl;
		else 
			cout<<"No carry operation."<<endl;
	}
	return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章