大数相乘

#include   
#include 
#include 
using namespace std;

int main()
{
	string s1, s2;
	while (cin >> s1 >> s2) {
		vector v1, v2;
		for (size_t i = 0; i != s1.size(); ++i)
			v1.push_back((int)(s1[s1.size() - 1 - i]) - '0');
		for (size_t j = 0; j != s2.size(); ++j)
			v2.push_back((int)(s2[s2.size() - 1 - j]) - '0');
		
		vector res(v1.size() + v2.size(), 0);
		for (size_t i = 0; i != v1.size(); ++i) {
			for (size_t j = 0; j != v2.size(); ++j) {
				int k = i + j;
				res[k] += v1[i] * v2[j];
				while (res[k] > 9) {
					res[k + 1] += res[k] / 10;
					res[k] %= 10;
					++k;
				}
			}
		}

		int len = res.size() - 1;
		while (len > 0 && res[len] == 0)--len;
		for (int i = len; i >= 0; --i)
			cout << res[i];
		cout << endl;
	}

	return 0;
}

你可能感兴趣的:(C++)