大数的乘法(C++)
题目:POJ 2398
|
Bull Math
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 Sample Output 12345679011110987654321 Source |
嘴笨,直接上代码。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std; const int maxn = 100;
void reverse(char a[])
{
int len = strlen(a);
for(int i = 0 ; i < len / 2; i++)
{
int temp = a[i];
a[i] = a[len - i - 1];
a[len - i -1] = temp;
}
}
int main()
{
char a[maxn],b[maxn];
int t[100] = {0};
//printf("Please enter 2 numbers: ");
scanf("%s%s",a,b);
reverse(a);
reverse(b);
if(strcmp(a,"0")==0||strcmp(b,"0")==0)
cout<<"0"<<endl;
else
{
int i,j;
for(i = 0; i <strlen(b); i++)
{
int cnt = 0;
for(j = 0; j < strlen(a); j++)
{
int temp = (b[i] - '0') * (a[j] - '0');
int tt= t[i+j] + temp + cnt;
t[j+i] = tt % 10;
cnt = tt / 10;
}
while(cnt != 0)
{
t[j+i] = cnt % 10;
cnt = cnt / 10;
j++;
}
}
for(int k = i + j - 2; k >= 0; k--)
{
cout<<t[k];
}
}
return 0;
}
大数的乘法(C++)的更多相关文章
- vector、string实现大数加法乘法
理解 vector 是一个容器,是一个数据集,里边装了很多个元素.与数组最大的不同是 vector 可以动态增长. 用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将 ...
- sdut2613(This is an A+B Problem)大数加法(乘法)
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...
- [acm 1001] c++ 大数加法 乘法 幂
北大的ACM 1001 poj.org/problem?id=1001 代码纯手动编写 - - #include <iostream> #include <cstdio> #i ...
- HDU——1042N!(大数阶乘乘法)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Subm ...
- 剑指offer编程题Java实现——面试题12相关题大数的加法、减法、乘法问题的实现
用字符串或者数组表示大数是一种很简单有效的表示方式.在打印1到最大的n为数的问题上采用的是使用数组表示大数的方式.在相关题实现任意两个整数的加法.减法.乘法的实现中,采用字符串对大数进行表示,不过在具 ...
- ACM学习历程—51NOD1028 大数乘法V2(FFT)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...
- ZZNUOJ-2154:单身狗线下聚会【求N个数的最小公倍数,会超longlong,大数乘法,Java】
2154: 单身狗线下聚会 题目描述 马上就到七夕节了,单身狗们决定聚一聚.但是它们沉迷B站上的lo娘,他们每沉迷 ai 单身狗时间(这是它们专业计时)后就会休息 单身狗时间.它们想找到一个时间正好他 ...
- Cut the Cake(大数相乘)
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly o ...
- hdu 1042 N!(大数的阶乘)
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
随机推荐
- Linux的磁盘分区(1)
分区命名: 1.Linux下的分区命名不同于windows下的命名,对硬盘如IDE硬盘采用类似/dev/hdxy的方式来命名,其中hd表示分区所在的设备类型,如IDE硬盘,x表示硬盘盘号(a为基本主盘 ...
- Relax NG 在Odoo中的应用
想必有些同学一定会奇怪,Odoo是如何将模块中的XML中的诸如record.menuitem是如何被组织和定义的,以及各种field的各种属性究竟有哪些,今天,我们就来一探究竟. Relax NG:“ ...
- mysql 的2个关于事务和安全性的参数
innodb_flush_log_at_trx_commit:(mysql写事物日志的方式) 0 log buffer 会每秒写入到日志文件中,并刷新到磁盘 (提交方式与事物无关,性能最好) 1 事 ...
- 解析私有IP地址和公网IP地址
局域网私有IP地址上外网的原理 IP地址分为两部分,网络号和主机号,这种分法应用在私有和公有IP地址上.一个局域网中,为了该局域网的安全,我们应用了私有IP地址,为了和Internet中的其他主机进行 ...
- JAVA6开发WebService (三)——几个概念
转载自http://wuhongyu.iteye.com/blog/808922 要了解WebService,光能写代码不行啊,这说说WebService最基本的概念. 首先WebService要知道 ...
- HTTP 战役 与 历史
导火线1992年,有一家公司Nombas 开发了一种叫C--的嵌入式脚本语言,后来觉得名字比较晦气,最终改名为scriptEase.而这种可以嵌入网页中的脚本的理念,成为日后移动互联网蓬勃发展的一块重 ...
- php Use of undefined constant的问题解决方式
在每个文件头上加 error_reporting(0); 或者 搜索php.ini: error_reporting = E_ALL 改为: error_reporting = E_ALL & ...
- GsonUtils.java
package com.vcredit.ddcash.batch.util; import java.util.ArrayList;import java.util.List; import org. ...
- ubuntu安装Python环境以及科学计算环境
参考:http://blog.csdn.net/a1311543690/article/details/ 1.sudo apt-get install python-pip pip是Python的一个 ...
- 使用eclipse开发servlet
package cn.itcast; import java.io.IOException; import javax.servlet.GenericServlet; import javax.ser ...