-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path大数相乘.cpp
89 lines (80 loc) · 1.73 KB
/
大数相乘.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<stdlib.h>
#include <string.h>
#include<stdio.h>
using namespace std;
string calculate(string a,string b);
int resultList[256];
int main(){
string fn,sn;
cout<<"输入第一个数字和第二个数字.\n";
cin>>fn>>sn;
string result;
result=calculate(fn,sn);
cout<<result<<endl;
}
string calculate(string a,string b){
int temp=0,i=0,j=0,sumOfZero=0,carryBit=0,fn,sn,min,k=0,moveStep=0,count=-1;
string flag,zeros,result;
if(a.length() > b.length()) flag = "b";//找出a,b字符串中较短的那个.
else flag = "a";
if(flag=="a"){ //若a较短,则在a前面补0,如a="123"->a="000123";
min = a.length();
sumOfZero = b.length() - a.length();
for(i=0;i<sumOfZero;i++){
zeros+="0";
}
a= zeros + a;
}
else{
min = b.length();
sumOfZero = a.length() - b.length();
for(i=0;i<sumOfZero;i++){
zeros+="0";
}
b= zeros + b;
}
for(i=(a.length()-1);i>=0;i--){
sn = b[i]-'0';
for(j=(a.length()-1);j>=0;j--){
fn = a[j]-'0';
temp = fn*sn+carryBit;
carryBit = 0;
if(temp>9){
carryBit = temp/10;
temp=temp%10;
}
resultList[j-k+min] = resultList[j-k+min] + temp;
}
if(carryBit!=0) {
moveStep++;
resultList[j-k+min] = resultList[j-1-k+min] + carryBit;
}
carryBit=0;
k++;
}
for(i=(a.length()+min)-1;i>=(min-moveStep);i--){
if(resultList[i]>9){
carryBit = resultList[i]/10;
resultList[i] = resultList[i]%10;
resultList[i-1] = resultList[i-1]+carryBit;
carryBit = 0;
}
}
for(j=0;j<a.length()+min;j++){
if(resultList[i]!=0){
count++;
break;
}
}
for(i=0;i<a.length()+min;i++)
{
if(resultList[i]!=0 && i!=count){
char temps[256];
temp = resultList[i];
itoa(temp,temps,10);
result+=temps;
}
}
return result;
}