标签:
大数A+B A-B A*B;
在A-B是要判断AB的大小,我们要用大数减小数;
下面是我们oj的一个A*B的题;
链接:http://acm.zznu.edu.cn/problem.php?id=1562
2
1 2
123456789 987654321
Case 1:
1 * 2 = 2
Case 2:
123456789 * 987654321 = 121932631112635269
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define N 1100 void change(char s[], int a[])///把字符串类型的s转化成数字倒着存入a中; { int j=0, len = strlen(s); for(int i=len-1; i>=0; i--) { a[j++] = s[i]-‘0‘; } } void Add(int a[], int b[], int sum[]) { for(int i=0; i<N-1; i++) { int m = sum[i]+a[i]+b[i]; sum[i] = m%10; sum[i+1] += m/10; } } void Sub(int a[], int b[], int c[]) { for(int i=0; i<N; i++) { if(a[i]-b[i] < 0)///借位; { c[i] = a[i]+10-b[i]; a[i+1] -= 1; } else c[i] = a[i] - b[i]; } } void Mul(int a[], int b[], int c[]) { int m; for(int i=0; i<N; i++) { for(int j=0; i+j+1<N; j++) { m = a[i]*b[j]; m+=c[i+j]; c[i+j] = m%10; c[i+j+1] += m/10; } } } int main() { int T, t=1, i, f, a[N], b[N], c[N]; char s1[N], s2[N]; scanf("%d", &T); while(T--) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); f =0; scanf("%s%s", s1,s2); int len1 = strlen(s1); int len2 = strlen(s2); /* if(len1<len2) { f=1; swap(s1, s2); } else { if(strcmp(s1, s2)<0) f=1, swap(s1, s2); }*////在减法中要用到这个; change(s1, a); change(s2, b); /// Add(a, b, c);/// + /*Sub(a, b, c);/// - if(f == 1) printf("-"); */ Mul(a, b, c);/// * for(i=N-1; i>0; i--) { if(c[i]!=0) break; } printf("Case %d:\n", t++); printf("%s * %s = ", s1, s2); for(int j=i; j>=0; j--) printf("%d", c[j]); printf("\n"); if(T!=0) printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhengguiping--9876/p/4864922.html