大数的乘法(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 ...
随机推荐
- GUI之绘画控制
一.绘制的动力 GUI的绘画是根据消息动作驱动的 主循环应该是 二.绘制的过程 绘画过程应该是这样的: 首先窗口存在上下层关系-> 在绘制时应该从底部窗口开始绘制,逐一绘制 下面是一些特殊的窗口 ...
- Linux watch 监控系统状态
1.linux下watch命令的基本用法 # watch --helpUsage: watch [-dhntv] [--differences[=cumulative]] [--help] [--in ...
- mongodb数据导入导出以及备份恢复
昨日在公司收到游戏方发来一个1G多的数据文件,要求导入联运账号中.细细一看,纳尼!文件竟然是BSON格式. 哇塞,这不是去年给大家分享的NoSql中的MongoDB的备份文件吗? 于是搭好环境 1.启 ...
- 关于 微软必应词典客户端(pc) 的案例分析
第一部分 调研,评测 ●评测 bug one 在词典界面中搜完单词后,将鼠标移到英文例句上的单词时,会显示对应的中文翻译,而当移到短语时则不对应中文翻译. bug two 用orc强力取词,查询如上图 ...
- [Android] adb.exe换了位置
好久没有做android开发了,今天重新下载了新的sdk,发现adb.exe从sdk/tools里面消失了,添加了系统环境变量的路径就还是没法调动adb.exe命令,网上搜了一下原理是存在了新版的sd ...
- 快速安装zabbix agent并部署监控
1.准备yum源: epel源:yum install -y zabbix22-agent 2.上传脚本: 上传脚本事先写好的监控脚本到/script/下面 3.修改配置文件:Server=10.10 ...
- ZK 父窗口与子窗口消息交互
父窗口代码: 前台(test.zul) <?page title="" contentType="text/html;charset=UTF-8"?> ...
- C++重载operator的示例
#include<iostream>#include<vector>using namespace std; class test{public: int v; / ...
- CCF关于NOIP2014复赛报名的通知
CCF关于NOIP2014复赛报名的通知 CCF NOIP2014复赛全部实行网上注册.报名.未通过网上报名的选手将不具备参赛和申诉资格. 系统注册须知: NOIP2014复赛注册时间:2014年 ...
- Android课程---Oracle VM VirtualBox出现不能为虚拟机打开一个新任务
因工作需要在Win7下增添了Win7虚拟系统,随着VirtualBox 4.326的版本更新,用户们也开始升级.一用户在升级后发现原来创建的虚拟机无法打开,提示信息为:不能为虚拟电脑win7打开一个新 ...