uva 10106 Product(高精度乘法)

这篇博客介绍了UVA 10106问题,要求计算两个不超过10250的整数的乘积。输入包含多组整数对,输出为它们的乘积。博主分享了使用高精度乘法解决此问题的思路。

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

 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<string.h>
using namespace std;

#define N 62505

struct bign{
	int len, s[N];
    
	bign(){
		memset(s, 0, sizeof(s));
		len = 1;
	}
    
	bign operator = (const char *num){
		len = strlen(num);
		for (int i = 0; i < len; i++)
			s[i] = num[len - i - 1] - '0';
		return *this;
	}
    
	bign operator * (const bign &c){
		bign sum;
		sum.len = 0;
        
		for (int i = 0; i < len; i++){

			int g = 0;
			for (int j = 0; j < c.len; j++){

				int x = s[i] * c.s[j] + g + sum.s[i + j];

				sum.s[i + j] = x % 10;

				g = x / 10;
			}

			sum.len = i + c.len;

			while (g){
				sum.s[sum.len++] = g % 10;
				g = g / 10;
				
			}
		}

		return sum;
	}

};

int main(){
	char str1[N], str2[N];
	while (cin >> str1 >> str2){

		bign num1, num2, sum;

		num1 = str1;
		num2 = str2;
		sum = num1 * num2;

		int bo = 0;
		for (int i = sum.len - 1; i >= 0; i--){

			if (sum.s[i] || bo){
				
				bo = 1;
				cout << sum.s[i];
			}
		}

		if (bo == 0)
			cout << bo;	
		cout << endl;
		
		memset(str1, 0, sizeof(str1));
		memset(str2, 0, sizeof(str2));
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值