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. newgrp---将当前登录用户临时加入到已有的组中

    Linux中的newgrp命令主要是将当前登录用户临时加入到已有的组中,用法如下: [linuxidc@localhost etc]$ newgrp grptest 上面命令的含义是将用户linuxi ...

  2. tomcat日志配置之一自带log

    问题 tomcat每次启动时,自动在logs目录下生产以下日志文件,且每天都会生成对应日期的一个文件,造成日志文件众多: localhost.2012-07-05.txt catalina.2012- ...

  3. 阿里云 CentOS7.4 环境安装mysql5.7

    1. 删除默认安装的数据库,无所谓的请略过 据说CentOS7.x版本会默认安装mariadb数据库,我有点强迫症,故卸载之: rpm -qa|grep mariadb yum remove mari ...

  4. 阿姆达尔定律(Amdahl's law)

    首先给出阿姆达尔定律的数学公式描述: S(N)=1(1−p)+pN p:程序中可并行部分的程序在单核上执行时间的占比: N:处理器的数目(总的核心数) S(N):程序在 N 个处理器(总核心数)相对在 ...

  5. 47.__if_not_exists语句

    #include <iostream> using namespace std; template<class T> class myclass { public: T t; ...

  6. codeforces 1037E. Trips(倒叙)

    题目传送门: 解题思路: 正着搞好像有点恶心. 反着搞. 一边删一边搞,从崩坏的地方开始,入度--. 最后dfs崩坏,更新答案. 注意要把边删掉防止重复崩坏. 代码: #include<cstd ...

  7. HDU——T 1576 A/B

    http://acm.hdu.edu.cn/showproblem.php?pid=1576 Time Limit: 1000/1000 MS (Java/Others)    Memory Limi ...

  8. arukas 的 Endpoint

    arukas 的 Endpoint 什么是端点 What is Endpoint arukas.io 的实例几乎每周都自动重新启动,当实例重新启动时,其端口会更改.IP地址和端口的平均寿命是一周,有时 ...

  9. activity-栈相关属性

    1.启动任务栈 第一种,动作设置为“android.intent.action.MAIN”,类别设置为“android.intent.category.LAUNCHER”,可以使这个ACT(activ ...

  10. amazeui学习笔记二(进阶开发3)--HTML/CSS规范Rules

    amazeui学习笔记二(进阶开发3)--HTML/CSS规范Rules 一.总结 1.am:以 am 为命名空间 2.模块状态: {命名空间}-{模块名}-{状态描述} 3.子模块: {命名空间}- ...