Skip to content

Commit

Permalink
增加大整数运算的示例实现
Browse files Browse the repository at this point in the history
  • Loading branch information
ecustsw committed Jul 13, 2020
1 parent a0893c8 commit 4068c71
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 3 deletions.
72 changes: 71 additions & 1 deletion SWBitOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,74 @@ long long SWBitOp::Mutiple(long long a, long long b, long long p)
b >>= 1;
}
return res;
}
}

vector<int> SWBitOp::add(vector<int>& l, vector<int>& r)
{
vector<int> res;
int bit = 0;
for (int i = 0; i < l.size() || i < r.size() || bit; i++)
{
if (i < l.size()) bit += l[i];
if (i < r.size()) bit += r[i];
res.push_back(bit % 10);
bit /= 10;
}

return res;
}

//besure content of l is bigger than content of r
vector<int> SWBitOp::sub(vector<int>& l, vector<int>& r)
{
vector<int> res;
int bit = 0;
for (int i = 0; i < l.size() || bit; i++)
{
bit = l[i] - bit;
if (i < r.size()) bit -= r[i];
res.push_back((bit + 10) % 10);
if (bit < 0)
bit = 1;
else
bit = 0;
}
while (res.size() > 1 && res.back() == 0) res.pop_back();

return res;
}

vector<int> SWBitOp::multi(vector<int>& l, int r)
{
vector<int> res;
int bit = 0;
for (int i = 0; i < l.size() || bit; i++)
{
if(i < l.size()) bit += l[i] * r;
res.push_back(bit % 10);
bit /= 10;
}
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}

vector<int> SWBitOp::div(vector<int>& l, int r,int &rem)
{
vector<int> res;
int bit = 0;
for (int i = l.size() - 1; i >= 0; i--)
{
if (i >= 0)
bit = bit * 10 + l[i];
res.push_back(bit / r);
bit %= r;
}
reverse(res.begin(),res.end());

while (res.size() > 1 && res.back() == 0) res.pop_back();

rem = bit;
return res;
}


10 changes: 10 additions & 0 deletions SWBitOp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include <vector>
using namespace std;
class SWBitOp
{
public:
Expand All @@ -10,5 +12,13 @@ class SWBitOp

//#2 a * b % p µÄÖµ(64bit)
static long long Mutiple(long long a, long long b, long long p);

static vector<int> add(vector<int> & l,vector<int>& r);

static vector<int> sub(vector<int>& l, vector<int>& r);

static vector<int> multi(vector<int> & l,int r);

static vector<int> div(vector<int> & l,int r,int& rem);
};

67 changes: 67 additions & 0 deletions Test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#include "src/Algo.h"
#include "CClock.h"
#include "SWBitOp.h"

void testQickSort(int src[],int n)
{
Expand Down Expand Up @@ -67,5 +70,69 @@ void testRangeOfNumber(int src[], int n,int k)

clock.end();

return;
}

void testAdd(vector<int>& l, vector<int>& r)
{
CClock clock;

auto res = SWBitOp::add(l,r);

for (int i = res.size() - 1; i >= 0; i--)
cout << res[i];

cout << endl;

clock.end();

return;
}

void testSub(vector<int>& l, vector<int>& r)
{
CClock clock;

auto res = SWBitOp::sub(l, r);

for (int i = res.size() - 1; i >= 0; i--)
cout << res[i];

cout << endl;

clock.end();

return;
}

void testMulti(vector<int>& l, int r)
{
CClock clock;

auto res = SWBitOp::multi(l, r);

for (int i = res.size() - 1; i >= 0; i--)
cout << res[i];

cout << endl;

clock.end();

return;
}

void testDiv(vector<int>& l, int r,int & rem)
{
CClock clock;

auto res = SWBitOp::div(l, r,rem);

for (int i = res.size() - 1; i >= 0; i--)
cout << res[i];

cout << endl << rem << endl;

clock.end();

return;
}
19 changes: 18 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <vector>
#include "MinStack.h"
using namespace std;

Expand All @@ -8,6 +9,10 @@ extern void testKMins(int src[], int n, int k);
extern void testMergeSort(int src[], int n, int tmp[]);
extern void testReversePairNum(int src[], int n, int tmp[], long long* pairs);
extern void testRangeOfNumber(int src[], int n,int k);
extern void testAdd(vector<int>& l, vector<int>& r);
extern void testSub(vector<int>& l, vector<int>& r);
extern void testMulti(vector<int>& l, int r);
extern void testDiv(vector<int>& l, int r, int& rem);

int main()
{
Expand All @@ -20,6 +25,9 @@ int main()

long long pairs = 0;

vector<int> l = {2,2,1};
vector<int> r = {3,2,1};

//testQickSort(f,sizeof(f) / sizeof(f[0]));

//testKMins(f,sizeof(f) / sizeof(f[0]),4);
Expand All @@ -28,7 +36,16 @@ int main()

//testReversePairNum(f, sizeof(f) / sizeof(f[0]), tmp, &pairs);

testRangeOfNumber(f, sizeof(f) / sizeof(f[0]),2);
//testRangeOfNumber(f, sizeof(f) / sizeof(f[0]),2);

testAdd(l,r);

testSub(r,l);

testMulti(l,30);

int remain = 0;
testDiv(l,3,remain);

delete [] tmp;
}
20 changes: 19 additions & 1 deletion 算法基础点.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,22 @@
有单调性一定可以二分,二分的场景不一定需要有单调性
本质是用来找边界,这个边界在左半边区间是满足的,在右半边区间是不满足的。因是整数区间,所以两个区间没有交点
对应两个点:最后一个满足条件1的点,第一个满足条件2的点
每次选择时,选择答案所在的区间,边界点不丢失
每次选择时,选择答案所在的区间,边界点不丢失

4 大整数运算
大整数的每一位都保存到数组中,数组的低位保存数的低位,在进位时,在数据最后一位加1就行,不用移动整个数组
数的表示及存储
A3 A2 A1 A0
+ B2 B1 B0

A3 A2 A1 A0
- B2 B1 B0
进位,0或者1

除法中余数

5 前缀和数组
一维 sum[i] = sum[i - 1] + A[i]; i从1开始,避免边界条件 快速求出一段区间的和
二维 快速求出子矩阵的和

差分数组的构造

0 comments on commit 4068c71

Please sign in to comment.