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. SQL Server UPDATE语句的用法详解

    SQL Server UPDATE语句用于更新数据,下面就为您详细介绍SQL Server UPDATE语句语法方面的知识,希望可以让您对SQL Server UPDATE语句有更多的了解. 现实应用 ...

  2. SQL 到 NOSQL 的思维转变

    转自:http://blogread.cn/it/article/3130?f=wb SQL 到 NOSQL 的思维转变 NOSQL系统一般都会宣传一个特性,那就是性能好,然后为什么呢?关系型数据库发 ...

  3. 常用HTML小部件

  4. Project Euler 30 Digit fifth powers

    题意:判断一个数 N 的每一位的5次方的和是否为其本身 ,求出所有满足条件的数的和 思路:首先设这个数 N 为 n 位,可以简单的判断一下这个问题的上界 10 ^ n <= 9 ^ 5 × n, ...

  5. 训练1-o

    给出2个N * N的矩阵M1和M2,输出2个矩阵相乘后的结果. Input 第1行:1个数N,表示矩阵的大小(2 <= N <= 100)第2 - N + 1行,每行N个数,对应M1的1行 ...

  6. MyBatis中的大于号小于号表示

    可以使用转义字符把大于号和小于号这种直接替换掉: select* from table where '字段1'>=10怎么表示,问题来啦 xml转义可以使用 根据这个规则上面的sql写法应该变成 ...

  7. 编写 Node.js Rest API 的 10 个最佳实践

    Node.js 除了用来编写 WEB 应用之外,还可以用来编写 API 服务,我们在本文中会介绍编写 Node.js Rest API 的最佳实践,包括如何命名路由.进行认证和测试等话题,内容摘要如下 ...

  8. Ubuntu 15.10配置OpenCV

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50635705 1 安装前准备 安装以下 ...

  9. Guava工具类

    原文链接:http://blog.csdn.net/mnmlist/article/details/53425865 Objects类 Objects类有几个比较不错的方法,toString.hash ...

  10. Java 序列化 和 反序列--by Vincent

    序列化: Java 提供了一种把对象序列化的机制. 即把一个对象可以被表示为一个字节序列.然后可以写入文件保存. 字节序列包括:      该对象的数据.有关对象的类型的信息和存储在对象中数据的类型. ...