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. Arguments Optional FreeCodeCamp

    function add() { if(typeof arguments[0] !== "number" || (arguments.length > 1 && ...

  2. java equals的用法

    equals方法,用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较.Object类中的equals方法内部使用的就是==比较运算符. package Xuexi; public cla ...

  3. CF div2 499 A. Stages

    Code: #include<cstdio> #include<algorithm> #include<iostream> using namespace std; ...

  4. 算法61---两个字符串的最小ASCII删除和【动态规划】

    一.题目: 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 = "eat" ...

  5. C语言基础 (10) 变量作用域,生命周期 内存结构

    01 课程回顾 1.指针数组 注意: 对于数组来说,在使用sizeof的时候a和&a[0]是不一样的, 虽然以%x打印出来他们都是地址 2.值传递 int a; fun(a); int *** ...

  6. Project Euler 28 Number spiral diagonals

    题意:给出一个 1001 × 1001 的矩阵,寻找每一圈四个顶点,并求出所有顶点的和 思路:只需要找到右上顶点数字的规律,然后每一圈四个顶点构成了一个等差数列,求个和即可 /************ ...

  7. PHP 中 call_user_func 的使用

    call_user_func函数类似于一种特别的调用函数的方法,使用方法如下 第一种情况: function set_max($a,$b) { if($a>$b) echo $a; else e ...

  8. UVA11752 The Super Powers

    /* UVA11752 The Super Powers https://vjudge.net/contest/153365#problem/Y 数论 注意a^n=b要用除法求,并且求得的n比实际大1 ...

  9. 洛谷 P1124 文件压缩

    P1124 文件压缩 题目背景 提高文件的压缩率一直是人们追求的目标.近几年有人提出了这样一种算法,它虽然只是单纯地对文件进行重排,本身并不压缩文件,但是经这种算法调整后的文件在大多数情况下都能获得比 ...

  10. [SharePoint2010开发入门经典]编译部署SPS WebPart

    本章概要: 1.理解web部件,什么时候需要创建一个 2.理解标准和可视web部件的不同 3.使用VS构建部署web部件