大数的乘法(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 ...
随机推荐
- base64编解码实现
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <string.h&g ...
- [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- MySql_设置编码
1.查看mysql编码(默认latin1) show variables like 'collation_%'; +--------------------------+--------------- ...
- HTTP协议 (六) 状态码详解
HTTP协议 (六) 状态码详解 HTTP状态码,我都是现查现用. 我以前记得几个常用的状态码,比如200,302,304,404, 503. 一般来说我也只需要了解这些常用的状态码就可以了. 如果 ...
- Cookie工具类
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet ...
- Go 语言学习
golang中国 http://www.golangtc.com/ 第三方github学习 https://github.com/Unknwon/go-fundamental-programmingh ...
- 使用context来传递数据,一个context是一系列变量
页面设计工作和python代码分离,所以我们引用模板来实现这个功能. 一.模板实例 下面是一个模板的实例: [python]<html><head><title>O ...
- 20145337实验三实验报告——敏捷开发与XP实践
20145337实验三实验报告--敏捷开发与XP实践 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 ** 实验步骤**### 敏捷开发与XP 软件工程包括下列领域:软件需求 ...
- win7安装virtualbox遇到的问题
今天用台式机的时候想装个virtualbox跑centos做测试用,结果centos始终装不上,vbox一直提示无法开启任务.重装vbox,以及手动点击安装xxx.inf文件,都不行. 以前用的win ...
- JVM性能监控与故障处理命令行工具
JDK命令行工具 Sun公司作为”礼物“赠送给JDK使用者的工具: 这些命令行工具大多是jdk/lib/tools.jar类库的一层薄包装,主要功能代码是在tools类库(不属于java的标准API) ...