大数相加
#include<bits/stdc++.h>
using namespace std;
int main() {
string a, b,ans;
cin >> a >> b;
int len1 = a.length();
int len2 = b.length();
if ((len1-len2)>0) {
for (int i = 0; i < (len1-len2); i++) {
b = '0' + b;
}
}
else {
for (int i = 0; i <(len2-len1); i++) {
a = '0' + a;
}
}
int temp;
int len = a.length();
int cf = 0;
for (int i =len-1 ; i >=0 ; i--) {
temp = a[i] - '0' + b[i] - '0' + cf;
cf = temp / 10;
temp %= 10;
ans = (char)(temp + '0') + ans;
}
if (cf != 0) ans = (char)(cf + '0') + ans;
cout << ans<<endl;
}
大数相乘
int main() {
string aa, bb;
int a[10001], b[10001], ans[10001] = { 0 };
cin >> aa >> bb;
int len1 = aa.length();
int len2 = bb.length();
for (int i = 0; i <= len1 - 1; i++) a[i] = aa[len1 - 1 - i] - '0';
for (int i = 0; i <= len2 - 1; i++) b[i] = bb[len2 - 1 - i] - '0';
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
ans[i + j] += a[i] * b[j];
}
}
for (int i = 0; i < len1 + len2 - 1; i++) {
if (ans[i] > 9) {
ans[i + 1] += ans[i] / 10;
ans[i] %= 10;
}
}
int len = len1 + len2-1;
while (ans[len]==0 && len > 0) len--;
for (int i = len; i >= 0; i--) cout << ans[i];
}
大数乘小数
void solve(int* k, int b) {
for (int i = 0,c=0; i < L; i++) {
c = c / 10 + k[i]*b;
k[i] = c % 10;
}
}
//K[0]=1;
//为一个大数,b为要乘的小数
n大于20的阶乘
string mut(string num, int n) {
string res;
int c = 0;
for (int i = num.size() - 1; i >= 0; i--) {
int temp = (num[i] - '0') * n + c;
c = temp / 10;
temp = temp % 10;
res.insert(res.begin(), temp + '0');
}
while (c != 0) {
char temp = c % 10 + '0'; res = temp + res;
c /= 10;
}
return res;
}
string solve(int n) {
if (n == 0 || n == 1) {
return "1";
}
return mut(solve(n - 1), n);
}
|