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

  1. 11111111111111
  2. 1111111111

Sample Output

  1. 12345679011110987654321

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

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. #define MAX 10000
  6.  
  7. using namespace std;
  8.  
  9. typedef struct bignum //定义大数类型
  10. {
  11. bignum(){memset(arr,0,sizeof(arr));length=0;} //初始化成员变量
  12. int arr[MAX*2];
  13. int length;
  14. }Bignum;
  15.  
  16. char s[MAX];
  17. char t[MAX];
  18.  
  19. Bignum atoi(char *s) //字符串转换为大数类型
  20. {
  21. Bignum res;
  22. int slen=strlen(s);
  23. for(int k=slen-1;k>=0;k--)
  24. {
  25. res.arr[k]=s[k]-'0';
  26. }
  27. res.length=slen;
  28. return res;
  29. }
  30.  
  31. //大数相乘
  32. Bignum quickmul(Bignum a,Bignum b)
  33. {
  34. Bignum res; //存放结果
  35. for(int i=0;i<a.length;i++)
  36. {
  37. for(int j=0;j<b.length;j++)
  38. {
  39. res.arr[i+j+1]+=a.arr[i]*b.arr[j]; //将a,b按位相乘
  40. }
  41. }
  42. res.length=a.length+b.length; //记得长度要更新
  43.  
  44. //处理进位
  45. int temp=0; //temp表示进位
  46. for(int i=res.length-1;i>=0;i--) //从后往前处理
  47. {
  48. int sum=res.arr[i]+temp;
  49. res.arr[i]=sum%10;
  50. temp=sum/10;
  51. }
  52. if(temp) //如果处理到最高位依然有进位,(左->右==>>高位->低位)
  53. res.arr[0]=temp;
  54. if(res.arr[0]==0) //如果res.arr[0]为0,把这个0去掉
  55. {
  56. for(int i=0;i<res.length-1;i++)
  57. res.arr[i]=res.arr[i+1];
  58. res.length--;
  59. }
  60.  
  61. return res;
  62. }
  63.  
  64. //输出大数
  65. void print(Bignum b)
  66. {
  67. for(int i=0;i<b.length;i++)
  68. cout<<b.arr[i];
  69. cout<<endl;
  70. }
  71.  
  72. int main()
  73. {
  74. while(cin>>s>>t)
  75. {
  76. Bignum a=atoi(s);
  77. Bignum b=atoi(t);
  78. print(quickmul(a,b));
  79. }
  80. return 0;
  81. }

  

大数乘法 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. bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...

  2. python的jieba分词

    # 官方例程 # encoding=utf-8 import jieba seg_list = jieba.cut("我来到北京清华大学", cut_all=True) print ...

  3. class一些内置方法

    一. __getattribute__ class Foo: def __init__(self,x): self.x=x def __getattr__(self, item): print('执行 ...

  4. [luogu4053 JSOI2007] 建筑抢修 (贪心 优先队列)

    传送门 题目描述 小刚在玩JSOI提供的一个称之为"建筑抢修"的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的损伤 ...

  5. React 手稿 - Component state

    Component state 实例: import React, { PureComponent } from 'react'; export default class extends PureC ...

  6. 参数化取值策略Random

    1.Random+Each iteration,跟顺序读取的结果唯一不同的就是这里是随机读取,取值是每次迭代取值   2.Random+Each occurrence,随机取值更新方式     3.R ...

  7. C#中的文本乱码问题

    文本乱码问题 //提供一种解决C#文本乱码的解决思路 //写入使用: string str; str = this.menu.Text; string fname = Application.Star ...

  8. java书籍推荐:《Java SE 6 技術手册》

    Java SE 6 技術手册 或  Java SE 6 技術手册 Java SE 6 技術手册 為什麼選擇用 Markdown?仅仅是單純把文件又一次排版太無聊了,不如趁這個機會學些新東西.所以我就藉 ...

  9. android继续探索Fresco

    我们接着上文继续说,上篇博客中我们已经知道了Fresco怎么用,也知道了它的非常多属性.可是非常多时候xml文件是不能满足你的要求的.这就须要你在代码中动态的改变显示的内容,今天我们就来探索一下怎样在 ...

  10. Wing IDE配置空格取代tab缩进+护眼背景色

    打开Wing IDE后,进入"编辑"列的"偏好设置"页面,如图1所看到的: 图1 设置用4个spaces取代tab, 如图2所看到的 图2 设置保护眼睛的绿色, ...