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 ans[600];
12
13 void BigInt(const char s[], int (&c)[600], int &len){
14     memset(c, 0, sizeof(c));
15     int L = strlen(s);
16     len = L / DLEN;
17     if(L%DLEN) len++;
18     int cnt = 0;
19     for(int i = L-1; i >= 0 ; i -= DLEN){
20         int k = i - DLEN + 1;
21         if(k < 0) k = 0;
22         int t = 0;
23         for(int j = k; j <= i; ++j)
24             t = t * 10 + s[j] - ‘0‘;
25         c[cnt++] = t;
26     }
27 }
28 void multi(){
29     int i, j;
30     for(i = 0; i < alen; ++i){
31         int up = 0;
32         for(j = 0; j < blen; ++j){
33             int t = a[i] * b[j] + ans[i+j] + up;
34             up = t / mod;
35             ans[i+j] = t % mod;
36         }
37         if(up != 0)
38             ans[i+j] = up;
39     }
40     ans_len = alen + blen;
41     while(ans[ans_len-1] == 0 && ans_len > 1)
42         ans_len--;
43
44     printf("%d", ans[ans_len-1]);
45     for(i = ans_len-2; i >= 0; i--)
46         printf("%04d", ans[i]);
47     puts("");
48 }
49 int main(){
50     scanf("%s%s", a1, b1);
51     BigInt(a1, a, alen);
52     BigInt(b1, b, blen);
53     multi();
54     return 0;
55 }

时间: 2024-08-26 14:04:15

51nod 1027大数乘法的相关文章

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

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 分

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

大数乘法

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

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

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

51 Nod 1028 大数乘法 V2【Java大数乱搞】

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

最短的计算大数乘法的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); }

【大数乘法】

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++