【学习笔记】hdu1042(高精度乘法)
来源:互联网 发布:防篡改软件 编辑:程序博客网 时间:2024/05/10 16:51
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1049
代码:
#include <cstdio> #include <cstring>#include <malloc.h> #include <iostream>using namespace std;void intToArray(int num, char* a){int i = 0, len;int t;while (num){a[i++] = num % 10 + '0';num /= 10;}len = i;for (i = 0; i < len / 2; i++) {t = a[i];a[i] = a[len - i - 1];a[len - i - 1] = t;}a[len] = 0;} void multiply(char* a, char* b, char* c) { int i, j, ca, cb, * s;ca = strlen(a); cb = strlen(b); s = (int*)malloc(sizeof(int)*(ca+cb)); for (i = 0; i < ca + cb; i++) s[i] = 0; for (i = 0; i < ca; i++) for (j = 0; j < cb; j++) s[i+j+1] += (a[i]-'0')*(b[j]-'0'); for (i = ca+cb-1; i >= 0; i--) if (s[i] >= 10) { s[i-1] += s[i] / 10; s[i] %= 10; } i = 0; while (s[i]==0) i++; for (j = 0; i < ca + cb; i++, j++) c[j] = s[i] + '0'; c[j] = '\0'; free(s); }int main(){int n, i;char ans[40000],b[10];while(~scanf("%d",&n)){ans[0] = '1'; ans[1] = 0;for (i = 1; i <= n; i++){intToArray(i,b);multiply(ans,b,ans);}cout << ans << endl;}return 0;}
其中multiply函数是个很有用的高精度乘法.
0 0
- 【学习笔记】hdu1042(高精度乘法)
- hdu1042 N! (高精度\大数阶乘)
- hdu1042 N!(高精度水过)
- HDU1042 高精度阶乘
- 高精度乘法(不压位)
- 高精度乘法(小数)
- 高精度乘法(正整数)
- 高精度模板(乘法)
- 高精度(大整数乘法)
- POJ 3289(高精度乘法)
- 大整数乘法(高精度)
- 高精度(乘法和加法)
- (C语言)高精度乘法
- POJ 1001(高精度乘法)
- Algorithm学习笔记 --- 求高精度幂(高精度)
- 高精度乘法
- 高精度乘法
- 高精度乘法
- EIA电平TTL电平
- 医学开源配准软件Elastix手册(中文翻译)——第五章 指南(重要配准参数)
- 算法导论 16章 贪心算法
- SqlServer 队列读取器代理在连接<subscriber>上的<subscriber_db>时遇到错误“No specified error”
- 【项目0 - 是春哥啊】
- 【学习笔记】hdu1042(高精度乘法)
- 四月份学习总结
- 带你**带你飞
- 第10周-一元方程类
- Android学习笔记——读取文件流文件路径
- 【初学PHP】关于XAMPP
- 5,13 第9 周项目1 复数的运算符重载
- 第九周项目三——分数类中的运算符重载(续)
- 【Matlab学习笔记】【图像滤波去噪】中值滤波