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. 关于MySQL日期操作函数 date_formate 的使用

    基本语法:DATE_FORMAT(date,format)说明:date 参数是合法的日期.format 规定日期/时间的输出格式.可以用的格式主要有格式 描述%a 缩写星期名%b 缩写月名%c 月, ...

  2. [poj 3666] Making the Grade (离散化 线性dp)

    今天的第一题(/ω\)! Description A straight dirt road connects two fields on FJ's farm, but it changes eleva ...

  3. UID和GID(详细说明)

    一.UID(User Identify)中文用户ID,相当于身份证一样,在系统中是唯一的. 用户分类centos6超级用户 UID=0 root普通用户 UID=500起 oldboy虚拟用户 UID ...

  4. vscode快捷键(lua开发)

    快速定位行:ctrl+g 查找:ctrl+f 格式化代码:ctrl+alt+f 快速查找到当前复制内容的第一次出现的位置ctrl+d 其他常用不一一列举了

  5. eclipse svn -- - --- appears to be part of a subversion 1.7 or greater....解决方法

    安装与svn1.7相兼容的flex Eclipse中的SVN(subclipse) 今天差点被TortoiseSVN1.7和subclipse弄崩溃... 还好最后弄好了,在此把方法写出来,以免其他人 ...

  6. 计算机网络系统--Microsoft Lync 与 腾讯通RTX 对比(转载)

    原文网址: http://it.vsharing.com/226.html ------------------------------- 上海大学统一通信平台现在尚未实施,一直在测试微软的Lync. ...

  7. Swift入门(四)——可选类型(Optionals)与断言(Assert)

    可选类型是什么? 首先看一个问题,Swift中String类型的变量有一个叫做toInt的方法,能够把String类型变量转换为Int类型变量. var stringValue = "5&q ...

  8. [GraphQL] Fetch Server Data and Client-side State in One Query using React Apollo + GraphQL

    In this lesson we look at how the Apollo @client directive can be used to fetch client-side state al ...

  9. 假设让我又一次设计一款Android App

    转载请注明出处: 本文来自aspook的博客:http://blog.csdn.net/ahence/article/details/47154419 开发工具的选择 开发工具我将选用Android  ...

  10. logstash tcp multihost output(多目标主机输出,保证TCP输出链路的稳定性)

    在清洗日志时,有一个应用场景,就是TCP输出时,须要在一个主机挂了的情况下,自已切换到下一个可用入口.而原tcp output仅支持单个目标主机设定.故本人在原tcp的基础上,开发出tcp_multi ...