https://www.acwing.com/problem/content/793/
给定两个正整数,计算它们的和。
位数100000
#include
#include
#include
#include
using namespace std;
const int N = 100010;
int A[N],B[N],C[N];
// A+B=C,tnt是AB两数的最大位数
// 返回C的位数(逆序输出即是答案)
int add(int a[],int b[],int c[],int tnt){
int t = 0 ;
for(int i = 1;i <= tnt;i++){
t += a[i] + b[i];
c[i] = t % 10;
t /= 10;
}
if(t){
c[++tnt] = 1;
}
return tnt;
}
int main(){
string a,b;
cin >> a >> b;
int t1 = 0;
for(int i = a.size() - 1;i >= 0;i--){
A[++t1] = a[i] - '0';
}
int t2 = 0;
for(int i = b.size() - 1;i >= 0;i--){
B[++t2] = b[i] - '0';
}
int t = add(A,B,C,max(t1,t2));
for(int i = t ;i >= 1;i--)
cout << C[i];
return 0;
}
https://www.acwing.com/problem/content/794/
给定两个正整数,计算它们的差,计算结果可能为负数。
位数100000
#include
#include
using namespace std;
const int N = 1e5+5;
bool cmp(vector<int> &A,vector<int> &B){
if(A.size() != B.size())
return A.size() >= B.size();
for(int i = A.size() - 1;i >= 0;i--)
if(A[i] != B[i])
return A[i] > B[i];
return true;
}
void trimZero(vector<int> &A){
while(A.back() == 0 && A.size() > 1)
A.pop_back();
}
vector<int> sub(vector<int> &A,vector<int> &B){
vector<int> C;
int t = 0;
for(int i = 0;i < A.size();i++){
t = A[i] - t;
if(i < B.size())
t -= B[i];
C.push_back((t + 10) % 10);
if(t < 0)
t = 1;
else
t = 0;
}
trimZero(C);
return C;
}
int main(){
string a,b;
cin >> a>> b;
vector<int> A,B,C;
for(int i = a.size() - 1;i >= 0;i--)
A.push_back(a[i] - '0');
for(int i = b.size() - 1;i >= 0;i--)
B.push_back(b[i] - '0');
trimZero(A);
trimZero(B);
if(cmp(A,B)){
C = sub(A,B);
}else{
C= sub(B,A);
printf("-");
}
for(int i = C.size() - 1;i >= 0;i--)
cout << C[i];
return 0;
}
https://www.acwing.com/problem/content/795/
给定两个正整数A和B,请你计算A * B的值。
1≤A的长度≤100000,
0≤B≤10000
#include
#include
using namespace std;
vector<int> mul(vector<int> &A, vector<int> &B) {
vector<int> C(A.size() + B.size(), 0); // 初始化为 0,且999*99最多 5 位
for (int i = 0; i < A.size(); i++)
for (int j = 0; j < B.size(); j++)
C[i + j] += A[i] * B[j];
int t = 0;
//消除进数
for (int i = 0; i < C.size(); i++) { // i = C.size() - 1时 t 一定小于 10
t += C[i];
C[i] = t % 10;
t /= 10;
}
while (C.size() > 1 && C.back() == 0)
C.pop_back(); // 必须要去前导 0,因为最高位很可能是 0
return C;
}
int main() {
string a, b;
cin >> a >> b; // a = "1222323", b = "2323423423"
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--)
A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--)
B.push_back(b[i] - '0');
auto C = mul(A, B);
for (int i = C.size() - 1; i >= 0; i--)
cout << C[i];
return 0;
}
https://www.acwing.com/problem/content/796/
给定两个非负整数A,B,请你计算 A / B的商和余数。
1≤A的长度≤100000 ,
1≤B≤10000
B 一定不为0
#include
#include
#include
#include
#include
using namespace std;
vector<int> div(vector<int> a,int b,int &r){
vector<int> c;
for(int i = 0;i < a.size();i++){
r = r * 10 + a[i];
c.push_back(r / b);
r = r % b;
}
reverse(c.begin(),c.end());
while(c.size() > 1 && c.back() == 0)
c.pop_back();
return c;
}
int main(){
string a;
int b;
cin >> a;
cin >> b;
int r = 0;
vector<int> x;
for(int i = 0;i < a.size();i++)
x.push_back(a[i] - '0');
auto z = div(x,b,r);
for(int i = z.size() - 1;i >= 0;i--){
cout << z[i];
}
cout << endl << r;
return 0;
}