标签:
1.29
UVA424
这道题就是普通的大数相加~
可以将数字存到字符数组里,也可以把数字存到整型数组里。记住》10要进位即可
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string.h> using namespace std; char a[105]; char sum[105]; void Reverse(char s[],int len){ for(int i=0;i<len/2;i++){ char temp=s[i]; s[i]=s[len-i-1]; s[len-i-1]=temp; } } int main(){ int maxx; scanf("%s",sum); maxx=strlen(sum); Reverse(sum,maxx); while(scanf("%s",a)!=EOF&&a[0]!=‘0‘){ int len=strlen(a); Reverse(a,len); //cout<<a<<"len"<<len<<maxx<<endl; int k; int sumlen=strlen(sum); maxx=max(sumlen,len); //cout<<maxx<<endl; for(int i=0;i<maxx;i++){ int sums; if(sum[i]==0) sums=a[i]-‘0‘; else if(a[i]==0) sums=sum[i]-‘0‘; else sums=a[i]+sum[i]-2*‘0‘; //cout<<sums<<endl; k=i; while(sums!=0){ if(k==i) sum[k]=sums%10+‘0‘; else if(sum[k]==0)sum[k]=sums%10+‘0‘; else sum[k]+=sums%10; sums/=10; k++; } } if(k>maxx) sum[k]=‘\0‘; else sum[maxx]=‘\0‘; } int lens=strlen(sum); for(int j=lens-1;j>=0;j--){ printf("%c",sum[j]); } cout<<endl; return 0; }
UVA10106
记住要每一位的乘法算完之后,再把结果的每一位数字大于10的进位!
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int a[505],b[505],c[505],sum[505]; char str1[505],str2[505]; int main(){ freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); while(scanf("%s",str1)!=EOF){ memset(sum,0,sizeof(sum)); int len1=strlen(str1); for(int i=0;i<len1;i++){ a[len1-i-1]=str1[i]-‘0‘; } scanf("%s",str2); if(str1[0]==‘0‘||str2[0]==‘0‘){ cout<<0<<endl; continue; } int len2=strlen(str2); for(int i=0;i<len2;i++){ b[len2-i-1]=str2[i]-‘0‘; } int k; for(int i=0;i<len1;i++){ for(int j=0;j<len2;j++){ int sums=a[i]*b[j]; k=j+i; do{ sum[k]+=sums%10; sums/=10; k++; }while(sums!=0); } } int w; for(int i=0;i<k;i++){ w=i; int sumss=sum[i]; do{ if(w==i)sum[w]=sumss%10; else sum[w]+=sumss%10; sumss/=10; w++; }while(sumss!=0); } for(int i=w-1;i>=0;i--){ printf("%d",sum[i]); } cout<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/Yvettey-me/p/5170231.html