C++大數乘法

c++版本的大數乘法

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<set>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
using namespace std;

string mul(string a,string b){
	string c(a.length() + b.length(),'0'); 
	for(int i = a.length() - 1;i >= 0;i--){
		for(int j = b.length() - 1;j >= 0;j--){
			int t = (a[i] - '0') * (b[j] - '0');
			t += c[i + j + 1] - '0';
			c[i + j + 1] = (t % 10) + '0';
			c[i + j] = (t / 10 + (c[i + j]) - '0') + '0'; 
		}
	}
	return c.substr(c.find_first_not_of("0"));
} 
int main(){
   cout << mul("120","30");
	return 0; 
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章