高精度乘法
来源:互联网 发布:婴儿翻译器软件下载 编辑:程序博客网 时间:2024/04/29 06:25
前言:
这个是我目前处理过的数得上的复杂题目,主要复杂在设计上,整个程序采用的是二维数组整体储存,再按列相加。
容易出错的地方有:
1.判断前导零。不过我这个程序巧妙的避开了去掉前导零的步骤,采用直接通法计算的模式。
2.模拟手工计算时向前进位。
3.单个数位相乘时判断越位,向前一位补数。
4.循环体内变量表示。
5.输出时去掉前导零。
下面是我的全部代码,启用里面注释掉的部分输出中间变量部分就能直观的看到计算的所有步骤。 -- 【Az】
#include <stdio.h>#include <stdlib.h>#include <string.h>char a[2500],b[2500],aa[2000],bb[2000];int re[2500][2500],plus = 0,i,result[2500];int main(){ int lenga,lengb; while(scanf("%s%s",a,b) != EOF) { memset(re,0,sizeof(re)); memset(result,0,sizeof(result)); lenga = strlen(a); lengb = strlen(b); plus = 0; int c = 2499,r = 0; // the length and width of re int j,k; for(r = 0,k = 0; r < lengb; r++,k++) { c = 2499 - k; for(i = lenga-1; i >= 0; i--) { re[r][c] = (b[lengb - 1 - r] - '0')*(a[i] - '0') + plus; if(re[r][c] >= 10) { plus = re[r][c] / 10; re[r][c] = re[r][c]%10; } else { plus = 0; } c--; } if(plus != 0) re[r][c] = plus; plus = 0; }// for(r = 0; r < lengb; r++)// {// for(j = 2500 - lenga - lengb; j < 2500; j++)// {// if(re[r][j] != 0)// printf("%d",re[r][j]);// else// printf("0");// }// printf("\n");// } //----------------------------------------------------------- add every number // 行数是lengb // 列数是lenga + lengb //printf("-----------------------\n"); int sum; for(i = 2499; i >= 2500 - lenga - lengb; i--) { sum = 0; for(j = 0; j < lengb; j++) { sum += re[j][i]; } sum += plus; if(sum >= 10) { plus = sum/10; result[i] = sum % 10; } else { plus = 0; result[i] = sum; } } int pd = 0; for(j = 2500 - lenga - lengb; j < 2500; j++) { if(result[j] != 0) pd++; } if(pd == 0) { printf("0\n"); goto end; } if(plus != 0) { printf("%d",plus); if(result[2500 - lenga -lengb] != 0) printf("%d",result[2500 - lenga -lengb]); for(j = 2500 - lenga - lengb+1; j < 2500; j++) { printf("%d",result[j]); } printf("\n"); } else { if(result[2500 - lenga -lengb] != 0) printf("%d",result[2500 - lenga -lengb]); for(j = 2500 - lenga - lengb+1; j < 2500; j++) { printf("%d",result[j]); } printf("\n"); }end: ; } return 0;}
0 0
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 高精度乘法
- 我的合肥java培训学习心得
- 菜鸟nginx源码剖析数据结构篇(五) 基数树 ngx_radix_tree_t
- Ubuntu12.04使用Dotdeb安装PHP5.4 / Nginx1.4/Redis2.6等新版本
- web项目统计在线人数
- Debugging a Deadlock(用户态、内核态)
- 高精度乘法
- VC项目不同后缀文件含义
- Linux常用命令(十九) - find之exec
- nginx - 缓存过期影响因素优先级分析
- 菜鸟nginx源码剖析数据结构篇(七) 哈希表 ngx_hash_t(下)
- springMVC3学习(二)--ModelAndView对象
- vim: remove trailing whitespaces
- 好久不见再见亦是朋友吧
- maven项目生成的war包在tomcat下运行报错