poj 2389 Bull Math(可做大数相乘模板)
2016-08-16 17:37
495 查看
Bull Math
Description
Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers
(no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros).
FJ asks that you do this yourself; don't use a special library function for the multiplication.
Input
* Lines 1..2: Each line contains a single decimal number.
Output
* Line 1: The exact product of the two input lines
Sample Input
Sample Output
Source
就是模拟乘法运算
代码菜鸟,如有错误,请多包涵!!!
如果有帮助记得鼓励我一下,谢谢!!!
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14488 | Accepted: 7445 |
Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers
(no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros).
FJ asks that you do this yourself; don't use a special library function for the multiplication.
Input
* Lines 1..2: Each line contains a single decimal number.
Output
* Line 1: The exact product of the two input lines
Sample Input
11111111111111 1111111111
Sample Output
12345679011110987654321
Source
就是模拟乘法运算
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; int sum[4000]; int a[2000], b[2000]; int Max; void BigNum(char ca[], char cb[]) { int i, j; int len_ca = strlen(ca); int len_cb = strlen(cb); for ( i = 0;i < len_ca; i++ ) { for ( j = 0;j < len_cb; j++ ) { sum[i+j] = sum[i+j]+a[i]*b[j]; } } int carry = 0; for ( i = 0;i <= Max; i++ ) { int t = sum[i]+carry; sum[i] = t%10; carry = t/10; } } void init(char ca[], char cb[]) { int i; memset(sum, 0, sizeof(sum)); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); int len_ca = strlen(ca); int len_cb = strlen(cb); Max = max(len_ca, len_cb)*2; for ( i = 0;i < len_ca; i++ ) { a[i] = ca[len_ca-i-1]-'0'; } for ( i = 0;i < len_cb; i++ ) { b[i] = cb[len_cb-i-1]-'0'; } } int main() { char ca[2000]; char cb[2000]; int i; scanf ( "%s %s", ca, cb ); init(ca, cb); // for ( i = 0;i < 4; i++ ) // { // printf ( "%d ", a[i] ); // printf ( "%d ", b[i] ); // } BigNum(ca, cb); for ( i = Max; i > 0; i-- ) { if (sum [i] != 0) break; } for ( i; i >=0; i-- ) { printf ( "%d", sum[i] ); } printf ( "\n" ); }
代码菜鸟,如有错误,请多包涵!!!
如果有帮助记得鼓励我一下,谢谢!!!
相关文章推荐
- final的作用随着所修饰的类型而不同
- Shell脚本 变量笔记
- JavaScript高级程序设计——第7章:函数表达式
- Codeforces Round #277.5 (Div. 2) C. Given Length and Sum of Digits...
- Android Device Monitor查看进程中的有多少个线程在搞
- Next Permutation
- ActivityManager(4)—isLowRamDevice ()
- 图文详解Java中class的初始化顺序
- Linux文件系统-02存储设备分区
- hibernate 通过Restrictions设置中文查询条件,不执行无结果集的问题
- javaee三
- 神经网络的matlab使用
- react img 标签必须闭合,否则报错
- Java 架构师推荐书籍
- 紧张:飞测独家のJmeter秘籍,限量发放
- oc和javascript互相调用
- 使用Zabbix批量监控网站可用性方案二
- android中values的良好实践
- javascript图片与加载处理基础详细讲解几种方法实现
- CAD+开发小结+交互+选择集+深度拷贝AcDbObjectId中指向的实体集+读取其他DWG文件