加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数乘法 10106 - Product

发布时间:2020-12-14 04:06:05 所属栏目:大数据 来源:网络整理
导读:UVa OJ Root?::?AOAPC I: Beginning Algorithm Contests (Rujia Liu)?::?Volume 1. Elementary Problem Solving?::?Big Number ?Product? The Problem The problem is to multiply two integers X,Y. ( 0=X,Y10 250 ) The Input The input will consist of a

UVa OJ

Root?::?AOAPC I: Beginning Algorithm Contests (Rujia Liu)?::?Volume 1. Elementary Problem Solving?::?Big Number

?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

大数乘法。通过一个个模拟运算

用两个循环来模拟乘法运算。

因为一个小细节,把运算的过程写错了。。wa了好多次

然后没有考虑到乘数为0.。。又贡献了几次wa

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctype.h>
char numa[300],numb[300];
int mul[600];
using namespace std;
int main ()
{
    int i,j,max;
    while(cin>>numa>>numb)
    {
        memset(mul,sizeof(mul));
        int lena=strlen(numa),lenb=strlen(numb);;
        for (i=0,j=lena-1; i<j; i++,j--)
        {
            char temp;
            temp=numa[i];
            numa[i]=numa[j];
            numa[j]=temp;
        }
        for (i=0,j=lenb-1; i<j; i++,j--)
        {
            char temp;
            temp=numb[i];
            numb[i]=numb[j];
            numb[j]=temp;
        }
        for (i=0; i<lenb; i++)
        {
            for (j=0; j<lena; j++)
            {
                mul[j+i]+=(numa[j]-'0')*(numb[i]-'0');
                if (mul[j+i]>=10)
                {
                    mul[j+i+1]+=mul[j+i]/10; //之前写成了mul[j+i+1]+=mul[j+i+1]/10; 一直wa。。。
                    mul[j+i]=mul[j+i]%10;
                }
            }
        }
        max=j+i+3;
        for (j=max; j>=0; j--)
            if (mul[j]!=0) break;
        if (j>=0)       //要考虑前置0的问题
            for (i=j; i>=0; i--)
                cout<<mul[i];
        else cout<<"0";
        cout<<endl;
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读