【基础】高精度乘法
来源:互联网 发布:于正 琼瑶 知乎 编辑:程序博客网 时间:2024/06/09 20:10
描述 Description 高精度乘法
输入:两行,每行表示一个非负整数(不超过10000位)
输出:两数的乘积。 样例输入 Sample Input 样例输出 Sample Output 时间限制 Time Limitation 各个测试点1s
输入:两行,每行表示一个非负整数(不超过10000位)
输出:两数的乘积。 样例输入 Sample Input 样例输出 Sample Output 时间限制 Time Limitation 各个测试点1s
我又菜了,交了两次才过。都是不做数据的错。
是因为读入数据压位写错了,对于bit的整倍数长度的数,要先把长度+1,因为会跳过一开始的零头位处理。
#include <cstdio>#include <string>#include <cstring>typedef long long ll;const long bit = 8;const ll bit10 = 100000000;void pocess(char* x,ll* y){long l = 0;while (x[++l]);l--;y[0] = l/bit+1;long rr = l%bit;for (long i=1;i<rr+1;i++){y[y[0]] = (y[y[0]]<<3)+(y[y[0]]<<1)+x[1]-'0';x++; l--;}rr = y[0];for (long i=1;i<l+1;i++){if (!((i-1) % bit)) rr --;y[rr] = (y[rr]<<3)+(y[rr]<<1)+x[1]-'0';x++;}}void multiply(ll* a,ll* b,ll* c){c[0] = a[0] + b[0];for (long i=1;i<a[0]+1;i++){for (long j=1;j<b[0]+1;j++){c[i+j-1] += a[i]*b[j];while (c[i+j-1] >= bit10){c[i+j] += c[i+j-1]/bit10;c[i+j-1] %= bit10;}}}while (!c[c[0]])c[0]--;}void output(ll* a){printf("%ld",long(a[a[0]]));for (long i=a[0]-1;i>0;i--){printf("%08ld",long(a[i]));}}char aa[10010];char bb[10010];ll a[10010];ll b[10010];ll c[20010];int main(){freopen("multiply.in","r",stdin);freopen("multiply.out","w",stdout);scanf("%s",aa+1);scanf("%s",bb+1);pocess(aa,a);pocess(bb,b);multiply(a,b,c);output(c);return 0;}
- 【基础】高精度乘法
- 【基础】高精度乘法
- ACM 3117 高精度练习之乘法(基础)
- 高精度基础之乘法格式1
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- poj 1946 Cow Cycling
- java中获取ServletContext常见方法
- 把单个模块集成到框架里面
- 用C语言解决(hanoi)汉诺塔问题——函数的递归调用
- 【读书笔记】《大学算法教程》——chpt5.分而治之
- 【基础】高精度乘法
- mysql存储过程详解
- 黑马程序员 19 Java基础加强-01-基础篇
- php文件上传类
- 精通BitmapData
- Linux磁盘自动挂载
- MON51通信协议和实现分析v1.2
- svn merge和branch
- 微软面试题目