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. Django02 Django基础知识

    一.内容回顾 1.web应用程序 2.HTTP协议 a.http协议特性 b.http请求格式 c.http响应格式 3.wsgiref模块 4.Django下载与简单应用 a.Django简介(MT ...

  2. MySQL安装部署

    MySQL安装部署 使用自动化脚本

  3. javaee 文件的复制

    package Shurushucu; import java.io.FileInputStream; import java.io.FileNotFoundException; import jav ...

  4. tree -l命令参考

    一.简介 Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing( ...

  5. with as递归调用

    一.递归调用--在代码中偶尔看到以记之,便于下次学习 https://blog.csdn.net/johnf_nash/article/details/78681060 --查询节点及其下所有子节点 ...

  6. 实验二:编写输出"Hello World!"

    1.首先打开eclipse这个软件,新建Java项目,执行“文件→ 新建→Java项目 ”菜单命令,打开新建Java对话框,在项目名的编辑框中输入项目名编写输出"Hello World!”, ...

  7. Project Euler 35 Circular primes

    题意:197被称为圆周素数,因为将它逐位旋转所得到的数:197/971和719都是素数.小于100的圆周素数有十三个:2.3.5.7.11.13.17.31.37.71.73.79和97.小于一百万的 ...

  8. HDU1079-Calendar Game 简单思维博弈··

    题意:给你1990,1.1----2001.11.4范围内的某一天,格式year month day  两人轮流操作: 1. day+1; 2.month + 1: 谁先达到2001.11.4号,谁获 ...

  9. P1828 香甜的黄油 Sweet Butter (spfa)

    题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  10. iptables防火墙和selinux

    iptables 存在以下两种方式: 一.service方式 查看防火墙状态: [root@centos6 ~]# service iptables status iptables:未运行防火墙 开启 ...