1027 大数乘法

给出2个大整数A,B,计算A*B的结果。

Input

第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0)

Output

输出A*B

Input示例

123456

234567

Output示例

28958703552

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4
 5 int judge(char* a)
 6 {
 7     int i;
 8     for (i = 0; i < strlen(a); ++i) {
 9         if (a[i] != ‘0‘)
10             return 0;
11     }
12     return 1;
13 }
14
15 void multiplicative(char* a, char* b)
16 {
17     int lena = strlen(a), lenb = strlen(b);
18     int len = lena + lenb;
19     int i, j;
20     int a1[1005]={0}, b1[10005]={0}, r[10005]={0};
21
22     for (i = 0; i < lena; ++i)
23         a1[i] = a[lena-i-1] - ‘0‘;
24     for (i = 0; i < lenb; ++i)
25         b1[i] = b[lenb-i-1] - ‘0‘;
26     for (i = 0; i < lena; ++i) {
27         for (j = 0; j < lenb; ++j) {
28             r[i+j] += a1[i] * b1[j];
29         }
30     }
31     for (i = 0; i < len; ++i) {
32         r[i+1] += r[i] / 10;
33         r[i] = r[i] % 10;
34     }
35     while (!r[len-1])
36         len--;
37     for (i = 0; i < len; ++i)
38         a[i] = r[len-i-1] + ‘0‘;
39     if (judge(a))
40         strcpy(a, "0");
41     printf("%s", a);
42 }
43
44 int main ()
45 {
46     char A[10005], B[10005];
47     int fa, fb;
48     scanf("%s%s", A, B);
49     multiplicative(A, B);
50     return 0;
51 }
时间: 2024-08-07 03:01:35

1027 大数乘法的相关文章

51 Nod 1027 大数乘法【Java大数乱搞】

1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1027 分

51nod 1027大数乘法

题目链接:51nod 1027大数乘法 直接模板了. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int N = 1001; 5 const int DLEN = 4; 6 const int mod = 10000; 7 int alen, blen; 8 int ans_len; 9 char a1[N], b1[N]; 10 int a[600], b[600]; 11 int a

51NOD 1027 大数乘法

1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 #include <bits/stdc++.h> using namespace std; #define clear(A) mems

大数乘法

1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 相关问题 大数加法 0 大数开平方 640 大数进制转换 320 大数除法 320 大数乘法 V2 80 代码: 1 #inclu

大数加法、大数乘法

大数加法 hdu1002 #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #i

最短的计算大数乘法的c程序

#include <stdio.h> char s[99],t[99]; int m,n; void r(int i,int c) { int j=0,k=i; while(k)c+=s[j++]*t[k---1]; if(i)r(i-1,c/10); printf("%d",c%10); } void main() { gets(s);gets(t); while(s[n])s[n++]-=48; while(t[m])t[m++]-=48; r(m+n-1,0); }

ACM学习历程—51NOD1028 大数乘法V2(FFT)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这个长度的大数肯定不行. 大数可以表示点值表示法,然后卷积乘法就能用FFT加速运算了. 这道题是来存模板的. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath>

【大数乘法】

1 #include<cstdio> 2 #include<cstring> 3 const int Len = 100; 4 void Mul(char a[],char b[],char c[])//大数乘法 5 { 6 int i,j; 7 int alen = strlen(a),blen = strlen(b); 8 memset(c,0,Len); 9 for(i = 0; i < alen; i++) 10 for(j = 0; j < blen; j++

大数乘法的几种算法分析及比较(2014腾讯南京笔试题)

转自:http://blog.csdn.net/chhuach2005/article/details/21168179 1.题目 编写两个任意位数的大数相乘的程序,给出计算结果. 2.题目分析 该题相继被ACM.华为.腾讯等选作笔试.面试题,若无准备要写出这种程序,还是要花一定的时间的.故,觉得有必要深入研究一下.搜索了网上的大多数该类程序和算法,发现,大数乘法主要有模拟手工计算的普通大数乘法,分治算法和FFT算法.其中普通大数乘法占据了90%以上,其优点是空间复杂度低,实现简单,时间复杂度为