Product

The Problem

The problem is to multiply two integers X, Y. (0<=X,Y<10250)

The Input

The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output

For each input pair of lines the output line should consist one integer the product.

Sample Input

12
12
2
222222222222222222222222

Sample Output

144
444444444444444444444444

大数相乘,网上有非常多模板,今下午练练手,自己写一遍,刚開始忽略了前置0。导致0*12=00,0*123=000,后来改了输出就ac了。

#include <iostream>
#include <cstring>
#define maxn 260
using namespace std;
int main()
{
char str1[maxn],str2[maxn];
while(cin>>str1>>str2)
{
int len1,len2;
len1=strlen(str1);
len2=strlen(str2); int s1[maxn],s2[maxn];
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2)); int i;
for(i=0;i<len1;i++)
s1[i]=str1[i]-'0';
for(i=0;i<len2;i++)
s2[i]=str2[i]-'0'; int sum[maxn+maxn];
memset(sum,0,sizeof(sum));
int j,k;
for(i=len2-1;i>=0;i--)
{
for(j=len1-1;j>=0;j--)
{
sum[i+j+1]=sum[i+j+1]+s2[i]*s1[j];
}
}
for(k=len1+len2-1;k>=0;k--)
if(sum[k]>=10)
{
int temp;
temp=sum[k]%10;
sum[k-1]=sum[k-1]+sum[k]/10;
sum[k]=temp;
} //if(sum[0]!=0)
// cout<<sum[0];
bool flag=0;
for(i=0;i<len1+len2-1;i++)
{
if(sum[i]!=0)
flag=1;
if(flag==1)
cout<<sum[i];
}
cout<<sum[len1+len2-1];
cout<<endl;
}
return 0;
}

UVA 10106 Product (大数相乘)的更多相关文章

  1. (高精度运算4.7.21)UVA 10106 Product(大数乘法)

    package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class UVA_10106 ...

  2. UVa 10106 Product 【大数相乘】WA

    虽然是错的代码,但是还是想贴出来,最开始WA发现是没有考虑到乘积为0的情况,后来把a*0,0*a,a*0---0(若干个0),0--0(若干个0)*a都考虑进去了:可是还是WA,实在不懂先留在这儿. ...

  3. UVa 10106 Product

    高精度乘法问题,WA了两次是因为没有考虑结果为0的情况.  Product  The Problem The problem is to multiply two integers X, Y. (0& ...

  4. UVA 10106 (13.08.02)

     Product  The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input T ...

  5. POJ 2389 Bull Math(水~Java -大数相乘)

    题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...

  6. 大数相乘算法C++版

    #include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...

  7. java版大数相乘

    在搞ACM的时候遇到大数相乘的问题,在网上找了一下,看到了一个c++版本的 http://blog.csdn.net/jianzhibeihang/article/details/4948267 用j ...

  8. Linux C/C++ 编程练手 --- 大数相加和大数相乘

    最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...

  9. Karatsuba乘法--实现大数相乘

    Karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.此算法主要用于两个大数相乘 ...

随机推荐

  1. syslog日志介绍

    一. syslog简介 syslog是一种工业标准的协议,可用来记录设备的日志.在UNIX系统,路由器.交换机等网络设备中,系统日志(System Log)记录系统中任何时间发生的大小事件.管理者可以 ...

  2. Input/output subsystem having an integrated advanced programmable interrupt controller for use in a personal computer

    A computer system is described having one or more host processors, a host chipset and an input/outpu ...

  3. Android使用蓝牙连接adb调试App

    使用WiFi连接Android设备调试APP的教程非常多,可是项目中须要使用蓝牙进行通信.所以牵扯使用蓝牙调用adb. 1.   将电脑蓝牙与手机进行配对(控制面板->设备和打印机->加入 ...

  4. hdoj-1870-愚人节的礼物(栈)

    愚人节的礼物 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. gullo.me 的 natvps

    gullo.me 的 natvps 1. 在下面的地址中输入内网 IP,查看许可的网络资源 https://hosting.gullo.me/plugin/support_manager/knowle ...

  6. golang 函数作为类型

    golang 函数作为类型 package main import "fmt" type A func(int, int) func (f A)Serve() { fmt.Prin ...

  7. 七、Docker+nginx

    原文:七.Docker+nginx docker run -p 80:80 --name nginx-v1.0.0 -v /usr/nginx/www:/www -v /home/docker/ngi ...

  8. [Javascript AST] 4. Continue: Report ESLint error

    const disallowedMethods = ["log", "info", "warn", "error", & ...

  9. CentOS6.X安装10G需要额外安装的软件包

    yum -y install libXp yum -y install libXp.i686 yum -y install libXtst.i686

  10. 关于Promise的详细总结

    1. 异步回调 1.1 回调地狱 在需要多个操作的时候,会导致多个回调函数嵌套,导致代码不够直观,就是常说的回调地狱 1.2 并行结果 如果几个异步操作之间并没有前后顺序之分,但需要等多个异步操作都完 ...