主题:[讨论]浙江大学的1874,怎么会wa?
huangsir
[专家分:0] 发布于 2005-11-17 20:12:00
我的这个程序为什么还会wa?
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
int list1[11],list2[11];
int total,temp;
string m,n;
while(cin>>m>>n)
{
if(m=="0"&&n=="0")
return 0;
int i;
for(i=0;i<11;i++)
{
list1[i]=0;
list2[i]=0;
}
for(i=0;i<m.length();i++)
list1[i]=m[i]-48;
for(i=0;i<n.length();i++)
list2[i]=n[i]-48;
total=0;
temp=0;
for(int a=0;a<11;a++)
{
if(list1[a]+list2[a]+temp>9)
{
temp=1;
total++;
}
else
temp=0;
}
if(total==0)
cout<<"No carry operation."<<endl;
else
cout<<total<<" carry operations."<<endl;
}
return 0;
}
回复列表 (共2个回复)
沙发
767 [专家分:220] 发布于 2005-11-18 16:42:00
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
if(s1[0]=='0'&&s2[0]=='0') break;
else
{
vector<int> v;
v.clear();
int count=0;
if(s1.length()==s2.length())
{
for(int i=s1.length()-1;i>=0;--i)
v.push_back((int(s1[i])-int('0'))+(int(s2[i])-int('0')));
for(int i=0;i<s1.length()-1;++i)
if(v[i]>9)
{
v[i+1]+=1;
++count;
}
if(v[v.size()-1]>9) ++count;
}
if(s1.length()>s2.length())
{
int l=s1.length()-s2.length();
for(int j=s2.length()-1;j>=0;--j)
v.push_back((int(s1[j+l])-int('0'))+(int(s2[j])-int('0')));
for(int j=l-1;j>=0;--j)
v.push_back(int(s1[j])-int('0'));
for(int j=0;j<v.size()-1;++j)
if(v[j]>9)
{
++count;
v[j+1]+=1;
}
if(v[v.size()-1]>9) ++count;
}
if(s2.length()>s1.length())
{
int l=s2.length()-s1.length();
for(int j=s1.length()-1;j>=0;--j)
v.push_back((int(s2[j+l])-int('0'))+(int(s1[j])-int('0')));
for(int j=l-1;j>=0;--j)
v.push_back(int(s2[j])-int('0'));
for(int j=0;j<v.size()-1;++j)
if(v[j]>9)
{
++count;
v[j+1]+=1;
}
if(v[v.size()-1]>9) ++count;
}
if(count==0) cout<<"No carry operation.\n";
else
{
if(count==1) cout<<"1 carry operation.\n";
else cout<<count<<" carry operations.\n";
}
}
}
}
因为这题要用到高精度啊
板凳
nopeak [专家分:4100] 发布于 2005-11-25 18:19:00
#include <iostream.h>
#include <string.h>
void change(int num[],char str[])
{
num[0]=strlen(str);
for(int i=1;i<=num[0];i++)
{
num[i]=str[num[0]-i]-'0';
}
}
int main()
{
char str1[12],str2[12];
while(cin >> str1 >> str2)
{
if(strcmp(str1,"0")==0 && strcmp(str2,"0")==0)break;
int carry(0),i;
int a[12]={0},b[12]={0},c[12]={0};
change(a,str1);
change(b,str2);
for(i=1;i<=(a[0]>b[0]? a[0]:b[0]);i++)
{
c[i]+=a[i]+b[i];
if(c[i]>9)
{
carry++;
c[i+1]+=c[i]/10;
c[i]%=10;
}
}
if(c[i]==0)c[0]=i-1;
else c[0]=i;
if(carry==0)cout << "No carry operation." << endl;
else if(carry==1)cout << "1 carry operation." << endl;
else cout << carry << " carry operations." << endl;
}
return 0;
}
我来回复