uva 10106 - Product 大数乘法
发布时间:2020-12-14 04:03:40 所属栏目:大数据 来源:网络整理
导读:#includecstdio#includecstring#includealgorithm#define MAX 600using namespace std;struct NUM{ int len; char s[MAX];}num,num_1,num_2;int N[MAX];int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int i,j; while(gets(num.s)!
#include<cstdio> #include<cstring> #include<algorithm> #define MAX 600 using namespace std; struct NUM { int len; char s[MAX]; }num,num_1,num_2; int N[MAX]; int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int i,j; while(gets(num.s)!=NULL) { memset(N,sizeof(N)); num.len = strlen(num.s); num_1.len = num_2.len = 0; for(i = num.len-1; i >= 0; i--) num_1.s[num_1.len++] = num.s[i]; for(; num_1.len>=0&&num_1.s[num_1.len-1]=='0';num_1.len--); gets(num.s); num.len = strlen(num.s); for(i=num.len-1; i >= 0; i--) num_2.s[num_2.len++] = num.s[i]; for(; num_2.len>=0&&num_2.s[num_2.len-1]=='0';num_2.len--); for(i = 0; i < num_1.len; i++) { for(j = 0; j < num_2.len; j++) { N[i+j] += (num_1.s[i]-'0')*(num_2.s[j]-'0'); } } int sum = 0; for(i = 0; i < num_1.len+num_2.len; i++) { sum += N[i]; N[i] = sum%10; sum /= 10; } for(i = num_1.len+num_2.len; i >=0&&N[i]==0;i--);//刚开始没注意这 直接附上MAX超过数组范围了 结果WA的几次 细节啊 if(i == -1)printf("0");//结果为零的情况一定要考虑 else for(; i >= 0; i--) printf("%d",N[i]); printf("n"); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |