UVA Product(大数乘法)

本文详细解析了一个用于实现整数乘法运算的C++代码片段。通过使用字符串表示整数并进行位移操作,该代码高效地计算了两个整数的乘积。适合初学者了解整数乘法的基本实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Product
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF


 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






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

using namespace std;

int main()
{
    char str1[3000],str2[3000];
    int d[3000],f[3000],c[6000];
    int len1,len2,len,w,e;
    while(scanf("%s",str1)!=EOF)
    {
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        memset(f,0,sizeof(f));
        scanf("%s",str2);
        len1=strlen(str1);
        len2=strlen(str2);
        if(len1<=len2)
        {
            len=len2;
        }
        else
        {
            len=len1;
        }
        w=len1;
        e=len2;
        for(int i=0; i<len; i++)
        {
            if(len1-1>=0)
            {
                d[i]=str1[len1-1]-'0';
                len1--;
            }
            else d[i]=0;
            if(len2-1>=0)
            {
                f[i]=str2[len2-1]-'0';
                len2--;
            }
            else f[i]=0;
        }
        for(int i=0; i<w; i++)
        {
            for(int j=0; j<e; j++)
            {
                c[i+j]=c[i+j]+d[i]*f[j];
            }
        }
        for(int i=0; i<4000; i++) //i=w+e;
        {
            if(c[i]>=10)
            {
                c[i+1]+=c[i]/10;
                c[i]=c[i]%10;
            }
        }
        int flag=0;
        for(int i=4000; i>=0; i--) //i=w+e;
        {
            if(flag||c[i])
            {
                flag=1;
                printf("%d",c[i]);
            }
        }
        if(flag==0)
        {
            printf("0");
        }
        printf("\n");


    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值