Skip to content

Commit

Permalink
Chapter 8~12.
Browse files Browse the repository at this point in the history
Signed-off-by: Cyitao <[email protected]>
  • Loading branch information
Cyitao committed Jul 18, 2016
1 parent 746d800 commit 5a2807b
Show file tree
Hide file tree
Showing 57 changed files with 748 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ch10 概率/蚂蚁习题/Ants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<iostream>
#include<vector>
using namespace std;
#define Mod 1000000007
class Ants {
public:
vector<int> collision(int n)
{
int down=pow(2.0,n),up=down-2;
int temp=gcd(up,down);
vector<int> res;
res.push_back(up/temp);
res.push_back(down/temp);
return res;
}
int gcd(int x,int y)
{
return (!y)?x:gcd(y,x%y);
}
};
Binary file added ch10 概率/蚂蚁习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions ch10 概率/足球比赛练习题/Championship.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>
#include<vector>
using namespace std;
#define Mod 1000000007
class Championship {
public:
vector<int> calc(int k)
{
vector<int> res;
int up=1,down=1,i=2*k-1;
while(i>0)
{
down*=i;
i=i-2;
}
i=k+1;
while(i>2)
{
up*=i;
i--;
}
int Com=gcd(down-up,down);
res.push_back((down-up)/Com);
res.push_back(down/Com);
return res;
}
int gcd(int x, int y)
{
return (!y)?x:gcd(y,x%y);
}
};
Binary file added ch10 概率/足球比赛练习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions ch10 概率/随机01练习题/Random01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<iostream>
#include<vector>
using namespace std;
class RandomP {
public:
static int f();
};

class Random01 {
public:
// ÓÃRandomP::f()ʵÏֵȸÅÂʵÄ01·µ»Ø
int random01()
{
int a,b;
while(1)
{
a=RandomP::f();
b=RandomP::f();
if(a!=b)
return (a>b)?0:1;
}
}
};
Binary file added ch10 概率/随机01练习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions ch10 概率/随机函数练习题/Random7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>
#include<vector>
using namespace std;
// 以下内容请不要修改
class Random5 {
public:
static int randomNumber();
};

class Random7 {
public:
int rand5() {
return Random5::randomNumber();
}
// 以上内容请不要修改


int randomNumber()
{
int a=5*(rand5()-1)+rand5()-1;
while(a>20)
a=5*(rand5()-1)+rand5()-1;
return a%7+1;
}
};
Binary file added ch10 概率/随机函数练习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions ch10 概率/随机区间函数练习题/RandomSeg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<iostream>
using namespace std;
class RandomSeg {
public:
// 等概率返回[0,1)
double f() {
return rand() * 1.0 / RAND_MAX;
}
// 通过调用f()来实现
double random(int k, double x)
{
double res=-1;
for(int i=0;i<k;i++)
{
double b=f();
res=(res>b)?res:b;
}
return res;
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions ch10 概率/随机数组打印练习题/RandomPrint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
#include<vector>
using namespace std;
class RandomPrint {
public:
vector<int> print(vector<int> arr, int N, int M)
{
vector<int> res;
for(int i=0;i<M;i++)
{
int pos=rand()%(N-i);
res.push_back(arr[pos]);
swap(arr[pos],arr[N-i-1]);
}
return res;
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ch11 大数据/哈希函数.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions ch12 动态规划/01背包练习题/Backpack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<iostream>
#include<vector>
using namespace std;
class Backpack {
public:
int maxValue(vector<int> w, vector<int> v, int n, int cap)
{
int **dp=new int*[n+1];
for(int i=0;i<n+1;i++)
dp[i]=new int[cap+1];
for(int i=0;i<n+1;i++)
dp[i][0]=0;
for(int i=0;i<cap+1;i++)
dp[0][i]=0;
for(int i=1;i<n+1;i++)
for(int j=1;j<cap+1;j++)
{
int temp=dp[i-1][j];
if(j-w[i-1]>=0)
temp=max(temp,dp[i-1][j-w[i-1]]+v[i-1]);
dp[i][j]=temp;
}
return dp[n][cap];
}
};

int main()
{
int a[8]={42,25,30,35,42,21,26,28};
int b[8]={261,247,419,133,391,456,374,591};
vector<int> arr(a,a+8);
vector<int> brr(b,b+8);
Backpack B;
cout<<B.maxValue(arr,brr,8,297);
return 0;
}
Binary file added ch12 动态规划/01背包练习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions ch12 动态规划/LCS练习题/LCS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<iostream>
#include<vector>
using namespace std;
class LCS {
public:
int findLCS(string A, int n, string B, int m)
{
int **dp=new int*[n];
for(int i=0;i<n;i++)
dp[i]=new int[m];
for(int i=0,temp=0;i<n;i++)
{
if(B[0]==A[i])
temp=1;
dp[i][0]=temp;
}
for(int i=0,temp=0;i<m;i++)
{
if(A[0]==B[i])
temp=1;
dp[0][i]=temp;
}
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
{
int m=max(dp[i-1][j],dp[i][j-1]);
if(A[i]==B[j])
m=max(dp[i-1][j-1]+1,m);
dp[i][j]=m;
}
return dp[n-1][m-1];
}
};

int main()
{
string A="1A2C3D4B56",B="B1D23CA45B6A";
LCS f;
cout<<f.findLCS(A,10,B,12);
return 0;
}
Binary file added ch12 动态规划/LCS练习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions ch12 动态规划/LIS练习题/LongestIncreasingSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<iostream>
#include<vector>
using namespace std;
class LongestIncreasingSubsequence {
public:
int getLIS(vector<int> A, int n)
{
int *dp=new int[n],res=0;
dp[0]=1;
for(int i=1;i<A.size();i++)
{
int max=0,j=0;
while(j<i)
{
if(A[j]<A[i]&&dp[j]>max)
max=dp[j];
j++;
}
dp[i]=max+1;
}
for(int i=0;i<A.size();i++)
if(res<dp[i])
res=dp[i];
return res;
}
};
Binary file added ch12 动态规划/LIS练习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions ch12 动态规划/台阶问题练习题/GoUpstairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include<iostream>
#include<vector>
using namespace std;
#define Mod 1000000007
class GoUpstairs {
public:
int countWays(int n)
{
int *dp=new int[n];
dp[0]=1;
dp[1]=2;
for(int i=2;i<n;i++)
dp[i]=(dp[i-1]+dp[i-2])%Mod;
return dp[n-1];
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 113 additions & 0 deletions ch12 动态规划/找零钱练习题/Exchange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include<iostream>
#include<vector>
using namespace std;
//动态规划
class Exchange {
public:
int countWays(vector<int> penny, int n, int aim)
{
if(n==0||aim<0)
return 0;
int **map=new int*[n];
for(int i=0;i<n;i++)
map[i]=new int[aim+1];
for(int i=0;i<n;i++)
for(int j=0;j<aim+1;j++)
map[i][j]=-1;
for(int i=0;i<n;i++)
map[i][0]=1;
for(int i=0;i<aim+1;i++)
if(i%penny[0]==0)
map[0][i]=1;
else
map[0][i]=0;
for(int i=1;i<n;i++)
for(int j=1;j<aim+1;j++)
if((j-penny[i])>=0)
map[i][j]=map[i][j-penny[i]]+map[i-1][j];
else
map[i][j]=map[i-1][j];
return map[n-1][aim];
}

};


/*记忆搜索
class Exchange {
public:
int countWays(vector<int> penny, int n, int aim)
{
if(n==0||aim<0)
return 0;
int **map=new int*[n];
for(int i=0;i<n;i++)
map[i]=new int[aim+1];
for(int i=0;i<n;i++)
for(int j=0;j<aim+1;j++)
map[i][j]=-1;
return process(penny,0,aim,map);
}
int process(vector<int> arr,int index,int aim,int **map)
{
int res;
if(arr.size()-1==index)
{
if(map[index][aim]==-1)
map[index][aim]=(aim%arr[index])?0:1;
return map[index][aim];
}
else
{
res=0;
int k=aim/arr[index];
for(int i=0;i<=k;i++)
{
if(map[index+1][aim-i*arr[index]]==-1)
map[index+1][aim-i*arr[index]]=process(arr,index+1,aim-i*arr[index],map);
res+=map[index+1][aim-i*arr[index]];
}
}
return res;
}
};*/

/*暴力搜索
class Exchange {
public:
int countWays(vector<int> penny, int n, int aim)
{
if(n==0||aim<0)
return 0;
return process(penny,0,aim);
}
int process(vector<int> arr,int index,int aim)
{
int res;
if(arr.size()-1==index)
{
res=(aim%arr[index])?0:1;
return res;
}
else
{
res=0;
int k=aim/arr[index];
for(int i=0;i<=k;i++)
res+=process(arr,index+1,aim-i*arr[index]);
}
return res;
}
};*/

int main()
{
int a[3]={1,2,3};
vector<int> arr(a,a+3);
Exchange e;
cout<<e.countWays(arr,3,3);

return 0;
}
Binary file added ch12 动态规划/找零钱练习题/捕获.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5a2807b

Please sign in to comment.