题目:POJ 2398

Bull Math
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13410   Accepted: 6903

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

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++)的更多相关文章

  1. vector、string实现大数加法乘法

    理解 vector 是一个容器,是一个数据集,里边装了很多个元素.与数组最大的不同是 vector 可以动态增长. 用 vector 实现大数运算的关键是,以 string 的方式读入一个大数,然后将 ...

  2. sdut2613(This is an A+B Problem)大数加法(乘法)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

  3. [acm 1001] c++ 大数加法 乘法 幂

    北大的ACM 1001 poj.org/problem?id=1001 代码纯手动编写 - - #include <iostream> #include <cstdio> #i ...

  4. HDU——1042N!(大数阶乘乘法)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  5. 剑指offer编程题Java实现——面试题12相关题大数的加法、减法、乘法问题的实现

    用字符串或者数组表示大数是一种很简单有效的表示方式.在打印1到最大的n为数的问题上采用的是使用数组表示大数的方式.在相关题实现任意两个整数的加法.减法.乘法的实现中,采用字符串对大数进行表示,不过在具 ...

  6. ACM学习历程—51NOD1028 大数乘法V2(FFT)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...

  7. ZZNUOJ-2154:单身狗线下聚会【求N个数的最小公倍数,会超longlong,大数乘法,Java】

    2154: 单身狗线下聚会 题目描述 马上就到七夕节了,单身狗们决定聚一聚.但是它们沉迷B站上的lo娘,他们每沉迷 ai 单身狗时间(这是它们专业计时)后就会休息 单身狗时间.它们想找到一个时间正好他 ...

  8. Cut the Cake(大数相乘)

      MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly o ...

  9. hdu 1042 N!(大数的阶乘)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. 使用 Eclipse C/C++ Development Toolkit 开发应用程序

    使用 Eclipse C/C++ Development Toolkit 开发应用程序 (转) 来自http://blog.csdn.net/favory/article/details/189080 ...

  2. jquery-uploadify 上传

    先从官网下载插件 http://www.uploadify.com/ 引入之后.... html.................... <!-- 上传 --> <div id=&q ...

  3. MVVM deep dive

    You can get a different instance each time by passing a different key to the GetInstance method. How ...

  4. awesome-scala

    https://github.com/lauris/awesome-scala Awesome Scala  A community driven list of useful Scala libra ...

  5. PHP filesystem attack vectors

    http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/ On Apr 07, 2008 I spoke with Kuza55 and ...

  6. Lua数据结构

    lua中的table不是一种简单的数据结构,它可以作为其他数据结构的基础,如:数组,记录,链表,队列等都可以用它来表示. 1.数组 在lua中,table的索引可以有很多种表示方式.如果用整数来表示t ...

  7. fopen的第一个参数不能有'\n'

    我刚才写了个程序, 需要操作两个文件.  我用fgets获取标准输入流stdin中的文件名, 然后用fopen打开. 结果编译器总是抱错.  调试了一下,发现fopen返回的是NULL. 我在此处加了 ...

  8. 数据库---MySQL常用函数总结

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x)    返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x)    返回大于或等于 ...

  9. usaco 2016 Feb 负载平衡

    题目大意:平面上一堆点,用两条平行于坐标轴的直线将其分为四部分,使得点数最多的一部分最少 第一维枚举,第二维三分,点集用两棵树状数组维护 #include<bits/stdc++.h> # ...

  10. mac 下面wireshark 找不到网卡

    终端上面,执行如下命令:   sudo chgrp admin /dev/bpf*   sudo chmod g+rw /dev/bpf* http://www.9upk.com/article/25 ...