模板1 - 基础算法(排序、二分、高精度、前缀和差分、双指针、位运算、离散化)
作者:
坞中客
,
2021-06-16 16:13:51
,
所有人可见
,
阅读 394
vector<int> add(vector<int> &a, vector<int>& b){
if(a.size() < b.size()) return add(b, a);
vector<int> c;
int t = 0;
for(int i = 0; i < a.size(); i++){
t += a[i];
if(i < b.size()) t += b[i];
c.push_back(t % 10);
t /= 10;
}
while(t){
c.push_back(t % 10);
t /= 10;
}
return c;
}
void out(vector<int> &a){
for(int i = a.size() - 1; i >= 0; i--) cout << a[i];
cout << endl;
}
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;
}
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;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}