总结一些算法
头文件
#define _ALG_H_
#include<vector>
#include<string>
#include<algorithm>
// fibonacci迭代法
long long fibonacci(int );
//fibonacci 递归法
long fiboRecursion(int );
//bubbleSort
void bubbleSort(std::vector<int>& );
//selectSort
void selectSort(std::vector<int>& );
//kmp
bool kmpAlgorithm(std::string s,std::string t);
//prefixFun
std::vector<int> prefixFun(std::string t);
//moveFun
void moveFun(std::vector<int>& p);
// dynamic programming
int minPathSum(std::vector<std::vector<int>>& grid);// m x n的网格
//quickSort
void quickSort(std::vector<int>&);
// 取一个整数的各位放入数组中,按照高位到低位的次数
std::vector<int> splitNum(int num);
// 求两个数的最大公约数,最小公倍数
std::vector<int> maxCodivisorMinCoMult(int m,int n);
// gossipAlgPI
double gossopAlgPI();
#endif // _ALG_H_
实现源码
#include<time.h>
#include"Alg.h"
long long fibonacci(int n){
std::vector<int>fibo(n,1);
for(int i=2;i<n;i++)
fibo[i]=fibo[i-1]+fibo[i-2];
return fibo[n-1];
}
void bubbleSort(std::vector<int>& arr){
int n = arr.size();
for(int i=0;i < n; i++){
for(int j =0 ; j < n-1-i; j++){
if(arr[j] > arr[j+1])
std::swap(arr[j],arr[j+1]);
}
}
}
void selectSort(std::vector<int>& arr){
int n = arr.size();
for( int i=0; i< n-1; i++ ){
for(int j = i; j < n;j++ ){
if(arr[j] < arr[i])
std::swap(arr[j],arr[i]);
}
}
}
std::vector<int> prefixFun(std::string t){
int n = t.size();
int j=0,k=1;
std::vector<int> prefix(n,0);
while( k <n ){
if ( t[k] == t[j] ){
j++;
}
else{
while( j > 0&& t[k] != t[j] ){
j = prefix[j-1];
}
if( t[k] == t[j] ){
j++;
}
}
prefix[k]=j;
k++;
}
moveFun(prefix);
return prefix;
}
void moveFun(std::vector<int>& p){
int n = p.size();
for( int i = n-1; i>0; i-- ){
p[i]=p[i-1];
}
p[0] = -1 ;
}
bool kmpAlgorithm(std::string s,std::string t){
int m = s.size();
int n = t.size();
int i = 0;
int j = 0;
std::vector<int>prefix = prefixFun(t);
while( i < m&&j<n ){
if(j==-1|| s[i] == t[j]){
j++;
i++;
}
else{
j = prefix[j];
}
}
if ( j == n ){
return true;
}
else{
return false;
}
}
int minPathSum( std::vector<std::vector<int>>& grid){
int m = grid.size();//行数
int n = grid[0].size();//列数
std::vector<std::vector<int>> minPath( m, std::vector<int>(n,grid[0][0]));
for(int i = 1;i < m;i++){
minPath[i][0] = minPath[i-1][0] + grid[i][0];
}
for(int j = 1;j < n;j++){
minPath[0][j] = minPath[0][j-1] + grid[0][j];
}
for( int ii = 1; ii < m; ii++ ){
for(int jj = 1; jj < n; jj++ ){
minPath[ii][jj] = std::min(minPath[ii-1][jj],minPath[ii][jj-1]) + grid[ii][jj];
}
}
return minPath[m-1][n-1];
}
void _qS(std::vector<int>& arr,int const left,int const right){
if(left < right){
int i = left;
int j = right + 1;
int pivot = arr[left];
do{
do ++i;while( arr[i] < pivot );
do --j;while( arr[j] > pivot );
if(i<j) std::swap(arr[i],arr[j]);
} while( i < j );
std::swap(arr[left],arr[j]);
_qS(arr,left,j-1);
_qS(arr,j+1,right);
}
}
void quickSort(std::vector<int>& arr ){
_qS(arr,0,arr.size()-1);
}
std::vector<int> splitNum(int num){
int scal=10;
std::vector<int> sn;
while(num/scal >= 0 ){
sn.insert(sn.begin(),num % scal);
num /= scal;
if(num==0) break;
}
return sn;
}
std::vector<int> maxCodivisorMinCoMult(int m,int n){
int product = m * n;
int maxCodivisor;
int minCoMult;
int r;
while( n != 0 ){
r = m % n;
m = n;
n = r;
}
maxCodivisor = m;
minCoMult = product / m;
std::vector<int> mami({maxCodivisor,minCoMult});
return mami;
}
double gossopAlgPI(){
double x,y;
int sum = 2140000000,circleN = 0;
srand(time(NULL));
for(int i = 1;i < sum;i++){
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
if((x*x+y*y)<=1)
circleN++;
}
return (double)4*circleN/sum;
}