列表

详情


PDD2. 大整数相乘

描述

有两个用字符串表示的非常大的大整数,算出他们的乘积,也是用字符串表示。不能用系统自带的大整数类型。

输入描述

空格分隔的两个字符串,代表输入的两个大整数

输出描述

输入的乘积,用字符串表示

示例1

输入:

72106547548473106236 982161082972751393

输出:

70820244829634538040848656466105986748

原站题解

C++ 解法, 执行用时: 1ms, 内存消耗: 368KB, 提交时间: 2017-09-02

#include<iostream>
#include<string>
using namespace std;
//移位进位法
string Mul(string left, string right)
{

	size_t Lsize = left.size();
	size_t Rsize = right.size();
	size_t Size = Lsize + Rsize;
	string res(Size, '0');

	int takevoer = 0;//进位
	int offset = 0;//移位

	size_t idx = 1, j = 1;
	for (idx = 1; idx <= Rsize; ++idx)
	{
		takevoer = 0;
		int rightnum = right[Rsize - idx] - '0';
		//计算每一位与left相乘
		for (j = 1; j <= Lsize; ++j)
		{
			char resBit = res[Size - j - offset] - '0';
			int num = rightnum * (left[Lsize - j] - '0') + takevoer + resBit;
			takevoer = num / 10;
			res[Size - j - offset] = num % 10 + '0';
		}
		if (takevoer != 0)
			res[Size - j - offset] = takevoer + '0';
		offset++;
	}

	//如果没有进位的话,res最高位没有数字
	if (res[0] == '0')
		res.erase(0, 1);
	return res;
}

int main()
{
	string s1, s2;
	cin >>s1 >> s2;
	string str=Mul(s1,s2);
	cout << str << endl;
}

C++ 解法, 执行用时: 1ms, 内存消耗: 372KB, 提交时间: 2017-08-12

#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main(){
	string s1,s2;
	cin>>s1>>s2;
	vector<int> num1(s1.length(),0);
	vector<int> num2(s2.length(),0);
	vector<int> ans(s1.length()+s2.length(),0);
	vector<int> ji(s1.length()+1,0);

	for(int i=s1.length()-1,j=0;i>=0;--i)
			num1[j++]=s1[i]-'0';
	for(int i=s2.length()-1,j=0;i>=0;--i)
			num2[j++]=s2[i]-'0';
	int cheng,jin,jin1,he,x,k;
	for(int i=0;i<num2.size();++i)
	{
		jin=0,cheng=0,jin1=0,he=0,x=0;
		for(int j=0;j<num1.size();++j)
		{
			cheng=num1[j]*num2[i]+jin;
			ji[x++]=cheng%10;
			jin=cheng/10;
		}
		ji[x]=jin;
		for(k=0;k<ji.size();++k)
		{
			he=ji[k]+ans[k+i]+jin1;
			ans[k+i]=he%10;
			jin1=he/10;
		}
		//ans[k+i]=jin1;
	}
    	while(ans[ans.size()-1]==0) ans.pop_back();
	for(int i=ans.size()-1;i>=0;--i)
		cout<<ans[i];

	//system("pause");


	return 0;
}

C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-10

#include <bits/stdc++.h>
using namespace std;

string multiply(const string &A, const string &B) {
    if (A.empty() || B.empty()) return "";
    string product(A.size() + B.size(), '0');
    for (int i = A.size() - 1; i >= 0; --i) {
        int carry = 0;
        for (int j = B.size() - 1; j >= 0; --j) {
            int tmp = product[i + j + 1] - '0' + (A[i] - '0') * (B[j] - '0') +carry;
            product[i + j + 1] = tmp % 10 + '0';
            carry = tmp / 10;
        }
        product[i] += carry;
    }
    size_t pos = product.find_first_not_of('0');
    if (pos != string::npos) return product.substr(pos);
    return "0";
}

int main() {
	string A, B;
	cin >> A >> B;
	string res = multiply(A, B);
	cout << res << endl;
	return 0;
}

C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-07

#include<string>
#include<iostream>
using namespace std;
const int L=1000;  
string mul(string,string);
int main(){
    string x,y;
    while(cin>>x>>y)
        cout<<mul(x,y)<<endl;
}
string mul(string a,string b) {  
    string s;  
    int na[L],nb[L],nc[L],La=a.size(),Lb=b.size(),i,j;
    fill(na,na+L,0);fill(nb,nb+L,0);fill(nc,nc+L,0);
    for(i=La-1;i>=0;i--) na[La-i]=a[i]-'0';
    for(i=Lb-1;i>=0;i--) nb[Lb-i]=b[i]-'0';  
    for(i=1;i<=La;i++)  
        for(j=1;j<=Lb;j++)  
        nc[i+j-1]+=na[i]*nb[j];
    for(i=1;i<=La+Lb;i++)  
        nc[i+1]+=nc[i]/10,nc[i]%=10;
    if(nc[La+Lb]) s+=nc[La+Lb]+'0';
    for(i=La+Lb-1;i>=1;i--)  
        s+=nc[i]+'0';
    return s;  
}

C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2017-09-04

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <string.h>
#include <math.h>
#include <iomanip>
#include <algorithm>
#include <map>

using namespace std;

int main(void)
{
	string str_1, str_2;
	while (cin >> str_1 >> str_2)
	{
		vector<int> num_1;
		vector<int> num_2;

		for (int i = str_1.size() - 1; i >= 0; i--)
			num_1.push_back((int)(str_1[i] - '0'));

		for (int i = str_2.size() - 1; i >= 0; i--)
			num_2.push_back((int)(str_2[i] - '0'));

		int length = num_1.size() + num_2.size();
		vector<int> result(length, 0);

		for (int i = 0; i < num_1.size(); i++)
		{
			for (int j = 0; j < num_2.size(); j++)
			{
				int temp = 0;
				temp = num_1[i] * num_2[j];
				if (temp >= 10)
				{
					result[i + j] += temp % 10;
					if (result[i + j] >= 10)
					{
						result[i + j + 1] += result[i + j] / 10;
						result[i + j] = result[i + j] % 10;
					}
					result[i + j + 1] += temp / 10;
				}
				else
				{
					result[i + j] += temp;
					if (result[i + j] >= 10)
					{
						result[i + j + 1] += result[i + j] / 10;
						result[i + j] = result[i + j] % 10;
					}
				}
			}
		}

		for (int i = result.size() - 1; i >= 0; i--)
		{
			if (result.back() == 0)
				result.pop_back();
			else 
			{
				if (result[i] >= 10)
				{
					result[i + 1] = result[i] / 10;
					result[i] = result[i] % 10;
				}
			}
		}
		for (int i = result.size() - 1; i >= 0; i--)
			cout << result[i];

		cout << endl;
	}
	// system("pause");
	return 0;
}

上一题