Bull Math
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14972   Accepted: 7695

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

11111111111111
1111111111

Sample Output

12345679011110987654321

题意:输入两个大数,输出它们相乘的结果。

代码:

#include <iostream>
#include <cstdio>
#include <cstring> #define MAX 10000 using namespace std; typedef struct bignum //定义大数类型
{
bignum(){memset(arr,0,sizeof(arr));length=0;} //初始化成员变量
int arr[MAX*2];
int length;
}Bignum; char s[MAX];
char t[MAX]; Bignum atoi(char *s) //字符串转换为大数类型
{
Bignum res;
int slen=strlen(s);
for(int k=slen-1;k>=0;k--)
{
res.arr[k]=s[k]-'0';
}
res.length=slen;
return res;
} //大数相乘
Bignum quickmul(Bignum a,Bignum b)
{
Bignum res; //存放结果
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
res.arr[i+j+1]+=a.arr[i]*b.arr[j]; //将a,b按位相乘
}
}
res.length=a.length+b.length; //记得长度要更新 //处理进位
int temp=0; //temp表示进位
for(int i=res.length-1;i>=0;i--) //从后往前处理
{
int sum=res.arr[i]+temp;
res.arr[i]=sum%10;
temp=sum/10;
}
if(temp) //如果处理到最高位依然有进位,(左->右==>>高位->低位)
res.arr[0]=temp;
if(res.arr[0]==0) //如果res.arr[0]为0,把这个0去掉
{
for(int i=0;i<res.length-1;i++)
res.arr[i]=res.arr[i+1];
res.length--;
} return res;
} //输出大数
void print(Bignum b)
{
for(int i=0;i<b.length;i++)
cout<<b.arr[i];
cout<<endl;
} int main()
{
while(cin>>s>>t)
{
Bignum a=atoi(s);
Bignum b=atoi(t);
print(quickmul(a,b));
}
return 0;
}

  

大数乘法 poj2389的更多相关文章

  1. 51nod 1027大数乘法

    题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...

  2. [POJ] #1001# Exponentiation : 大数乘法

    一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: ...

  3. 用分治法实现大数乘法,加法,减法(java实现)

    大数乘法即多项式乘法问题,求A(x)与B(x)的乘积C(x),朴素解法的复杂度O(n^2),基本思想是把多项式A(x)与B(x)写成 A(x)=a*x^m+b B(x)=c*x^m+d 其中a,b,c ...

  4. HDOJ-1042 N!(大数乘法)

    http://acm.hdu.edu.cn/showproblem.php?pid=1042 题意清晰..简单明了开门见山的大数乘法.. 10000的阶乘有35000多位 数组有36000够了 # i ...

  5. 51 Nod 1027 大数乘法【Java大数乱搞】

    1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度  ...

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

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...

  7. hdu_1042(模拟大数乘法)

    计算n! #include<cstring> #include<cstdio> using namespace std; ]; int main() { int n; whil ...

  8. (母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. 大数乘法|2012年蓝桥杯B组题解析第六题-fishers

    (9')大数乘法 对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的 ...

随机推荐

  1. python编写简单的html登陆页面(4)

    python编写简单的html登陆页面(4)   1  在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将动态态分配数据,建立表格,存放学生信息 2 实现的效果如下: 3  动 ...

  2. 洛谷 P1540 乌龟棋

    第一感觉是定义状态f[n][i][j][k][kk],但这样空间和时间都承受不下.我们可以设状态为f[i][j][k][kk],这样可以省掉一个n,因为我们依据行走步数可以直接算出行走距离. Code ...

  3. redis过期key删除

    LZ一开始配置到启动类里面,结果出现了主线程阻塞的情况. 如下是流程: 首先修改配置文件redis.conf中的:notify-keyspace-events Ex,默认为notify-keyspac ...

  4. 编译qemu

    el7上编译 git clone git://git.qemu-project.org/qemu.git ./configure --target-list=x86_64-softmmu --cpu= ...

  5. Problem 11

    Problem 11 # Problem_11.py """ In the 20×20 grid below, four numbers along a diagonal ...

  6. Vue -- element-ui el-table 的合计在第一行显示并可点击

    使用element-ui el-table 中有这样一个需求,需要将合计放在表格内容的第一行,并且点击合计可跳转到其它页面! 框架中提供了合计的属性方法,这样可以进行数值求和及自定义求和,但是,合计那 ...

  7. 【hdu 6444】Neko's loop

    [链接] 我是链接,点我呀:) [题意] 给你一个序列. 你可以选择起点i. 然后每次往右跳k次. 得到下一个值a[i+k];. 问你跳m次能得到的最大值ma是多少. 如果>=s输出0 否则输出 ...

  8. ZOJ 2314 Reactor Cooling

    Reactor Cooling Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  9. socketpair和pipe的区别

    http://blog.csdn.net/bingqingsuimeng/article/details/9055499 管道pipe是半双工的,pipe两次才能实现全双工,使得代码复杂.socket ...

  10. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...