poj 2562

来源:百度知道 编辑:UC知道 时间:2024/05/24 22:50:26
#include<stdio.h>
#include<string.h>
int main()
{
char a[4][15];
int yu,count,lon1,lon2,n,lon;

while(scanf("%s%s",a[0],a[1]),(a[0][0]!='0' || a[1][0]!='0' || a[0][1] || a[1][1]))
{
count=0;
yu=0;

lon1=strlen(a[0]);
for(n=0;n<lon1;n++)
strcpy(&a[2][10-n],&a[0][lon1-1-n]);
for(n=0;n<11-lon1;n++)
a[2][n]='0';
lon2=strlen(a[1]);
for(n=0;n<lon2;n++)
strcpy(&a[3][10-n],&a[1][lon2-1-n]);
for(n=0;n<11-lon2;n++)
a[3][n]='0';
if(lon1>lon2)
lon=lon1;
else
lon=lon2;
for(n=0;n<lon;n++)
{
if(a[2][10-n]+a[3][10-n]+yu-'0'*2>9)
{
count++;
yu=1;
}
else
yu=0;
}
if(count==0)
printf("No carry operation.\n");
else if(count==1)
puts(&

AC的代码
//#include"stdafx.h"
#include<iostream>
#include<string>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
int carrtime(string& s,string& t){
int m=max(s.length(),t.length());
s=string(m-s.length(),'0')+s;
t=string(m-t.length(),'0')+t;
int ret=0,temp=0;
for (int i=m-1;i>=0;--i)
if(s[i]-'0'+t[i]-'0'+temp>9)
temp=1,ret++;
return ret;
}

int main(){
for (string s,t;cin>>s>>t&&(s!="0"||t!="0");)
{
int p=carrtime(s,t);
if(p==0)
cout<<"No carry operation."<<endl;
else if(p==1)
cout<<"1 carry operation."<<endl;
else
cout<<p<<" carry operations."<<endl;
}
}