随笔分类 - Algorithm
摘要:View Code 1 //日期处理 2 3 //每个月的天数 4 int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 5 6 char* week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; 7 8 typedef struct Date 9 {10 int year;11 int month;
阅读全文
摘要:View Code 1 //辗转相除法 2 //设k=x/y,b=x%y,则x=ky+b 3 //求x和y的最大公约数,也就是求y和b的最大公约数 4 int gcd(int x,int y) 5 { 6 while(y) 7 { 8 int b=x%y; 9 x=y;10 y=b;11 }12 return x;13 }View Code 1 int gcd(int x,int y) 2 { 3 while(x!=y) 4 { 5 if(x>y) 6 x...
阅读全文
摘要:Rotate a one-dimensional vector of n elements left by i positions.Solution1 1 void Solution(char* arr,int n,int rotdist) 2 { 3 int i; 4 //循环n和rotdist的最大公约数次 5 for(i=0;i<gcd(n,rotdist);++i) 6 { 7 char t=arr[i]; 8 int index=i; 9 while((index+rotdist)%n!=i)10 ...
阅读全文
摘要:View Code 1 //Solution1:对1到n的每个数进行拆分 2 int Solution(int n) 3 { 4 int count=0; 5 for(int i=1;i<=n;++i) 6 { 7 j=i; 8 while(j) 9 {10 if(j%10==1)11 count++;12 j=j/10;13 }14 }15 return count++;16 }17 18 //考虑n每一位c...
阅读全文
摘要:View Code 1 //有n个数表示ID,其中有一个ID出现的次数超过总数n的一半,求出这是哪个ID 2 3 //Solution1:对n个数进行排序,然后再扫描一遍排好序的ID列表,统计各个ID出现的次数,如果某个ID出现的次数超过总数n的一半,那么就输出这个ID 4 5 //Solution2:对n个数进行排序,则这个有序列表中的第n/2项一定会是要找的ID 6 7 //Solution3:每次删除两个不同的ID,那么在剩下的ID列表中,要找的ID出现的次数仍然超过总数的一半 8 Type Find(Type* ID,int n) 9 {10 int i,count;1...
阅读全文
摘要:View Code 1 //n!进行质因数分解,n!=2^x+3^y+5^z...所以末尾0的个数为min(x,z),又因为x>z,所以只要求出z的值即可 2 int Solution(int n) 3 { 4 int count=0; 5 for(int i=1;i<=n;++i) 6 { 7 int j=i; 8 while(j%5==0) 9 {10 count++;11 j=j/5;12 }13 return count;14 ...
阅读全文
摘要:View Code 1 //普通解法 2 int Solution(int v) 3 { 4 int count=0; 5 while(v) 6 { 7 if(v%2==1) 8 count++; 9 v=v/2;10 }11 return count;12 }13 14 //向右移位+与操作15 int Solution(int v)16 {17 int count=0;18 while(v)19 {20 if(v&0x1)21 c...
阅读全文
摘要:View Code 1 //---------------------API------------------------------ 2 3 4 //downloads a block from Internet sequentially in each call 5 //return true, if the entire file is downloaded, otherwise false. 6 bool GetBlockFromNet(Block* out_block); 7 8 //writes a block to hard disk 9 bool W...
阅读全文
摘要:View Code View Code 1 Build a sorted array consisting of start time and finish time of each event2 traverse3 if(element_type==begin)4 add a new room5 else6 delete a room 1 //Get the first finish time of rooms 2 int getFirst(int n,int* RoomFinish) 3 { 4 int first=0; 5 for(int i=1;i<n;++...
阅读全文
摘要:时间复杂度较差的一个View Code 1 int BestSolution(int* arr,int n) 2 { 3 int idx=1; 4 int min=0; 5 for(int i=1;i<n;++i) 6 min+=arr[i]*i; 7 for(int i=1;i<n;++i) 8 { 9 int calc=0;10 for(int j=0;j<i;++j)11 calc+=arr[j]*(i-j);12 for(int j=i+1;j<n;++j)...
阅读全文
摘要:动态规划解之View Code 1 /* 2 总共n中饮料,每种饮料表示为(S[i],V[i],C[i],H[i],B[i]),S表示名称,V表示容量,C表示可能的最大容量,H表示满意度,B表示实际购买量 3 V[i]*B[i]求和=V的情况下,H[i]*B[i]求和最大化 4 */ 5 6 #include <iostream> 7 using namespace std; 8 9 struct Beverage10 {11 int volumn;12 int maxOffer;13 int satisfication;14 int purchase;1...
阅读全文
摘要:首先想到的是贪心算法,结果贪心策略在这里行不通- -|贪心 1 #include <iostream> 2 using namespace std; 3 4 int books[5]; 5 int price=8; 6 int categorys; 7 float consumption=0.0; 8 float onSales5=0.25; 9 float onSales4=0.2;10 float onSales3=0.1;11 float onSales2=0.05;12 13 int LargestCategory(int *arr,int n)14 {15 int cnt
阅读全文
摘要:先想了一个最简单的方法:首先对n个烙饼进行处理,找到最大的那个烙饼,将其之上的进行翻转,然后对前n个烙饼堆进行翻转;第二次操作对上面n-1个烙饼进行操作,还是找到n-1个中最大的,将其之上的进行翻转,然后对前n-1个烙饼堆进行翻转。。。这样总共进行n-1次,每次翻转两次orzView Code 1 #include <iostream> 2 using namespace std; 3 4 int a[10]; 5 6 void Create_Array() 7 { 8 cout <<"input ten numbers:"; 9 for(int i
阅读全文
摘要:熟悉位操作符和位的存储方法View Code 1 #include <stdio.h> 2 3 #define LMASK (255 << 4) 4 #define RMASK (255 >> 4) 5 #define LGET(d) ((d&LMASK) >> 4) 6 #define RGET(d) (d&RMASK) 7 #define LSET(d,n) (d=((d&RMASK) | (n << 4))) 8 #define RSET(d,n) (d=((d&LMASK) | n)) 9 10
阅读全文
摘要:1、朴素的字符串匹配算法伪代码View Code 1 NAIVE-STRING-MATCHER(T,P)2 n=length(T)3 m=length(P)4 for(s=0;s<=n-m;++s)5 if(P[1...m]==T[1+s,...,m+s])6 print"Pattern occurs"View Code 1 char* T="acaabcsfsdfserfsdfsfwserwerfw"; 2 char* P="aab"; 3 4 void Naive_String_Matcher()...
阅读全文
摘要:定义:有向图G=(V,E),G的传递闭包定义为图G*=(V,E*),其中E*={(i,j):图G中存在一条从i到j的通路}方案1:对E中每条边赋以权值1,然后运行Floyd-Warshall算法。如果从顶点i到顶点j存在一条路径,则dij < n,否则dij=INFINITY.方案2:如果图G中从顶点i到顶点j存在一条通路,且其所有中间顶点均属于集合{1,2,...,k},则定义tij(k) 为true,否则为false。我们把边(i,j)加入E*中当且仅当tij(n)= true递归式为tij(0) =false if i!=j and (i,j)不属于E or true if i=j
阅读全文
摘要:简单路径p={v1,v2,...,vl}上的中间顶点是除v1和vl以外p上得任何一个顶点。令dij(k)为从顶点i到顶点j、且满足所有中间顶点皆属于{1,2,...,k}的一条最短路径的权值。当k=0时,从i到j根本不存在中间顶点。递归式为dij(k)=wij if k=0 or min(dij(k-1) , dik(k-1) + dkj(k-1)) if k>=1floyd 1 int D[5][25]; 2 3 void Floyd_Warshall(const Graph* g) 4 { 5 for(int i=0;i<25;++i) 6 D[0][i]=g->...
阅读全文
摘要:设lij(m) 是从顶点i到顶点j的至多包含m条边的任何路径的权值最小值。当m=0时,lij(0) =0 if i=j or INFINITY if i!=j递归定义lij(m) =min 1=<k<=n{lik(m-1) + wkj}实际上从i到j的最短路径至多包含n-1条边,因此lij(n-1)即为所要求的值。注意L(1)=W递归的函数如下View Code 1 EXTEND-SHORTEST-PATH(L,W)2 L'=new Matrix3 for(i=1;i<=n;++i)4 for(j=1;j<=n;++j)5 ...
阅读全文
摘要:1、Bellman-Ford算法该算法能解决单源最短路径问题,即使存在负权边的情况下。运行Bellman-Ford算法可以返回一个布尔值,如果为true,则说明图中不包含从源点可达的负权回路,算法将产生最短路径及其权值;如果为false,则说明图中包含从源点可达的负权回路,该问题无解。View Code 1 int d[50]; 2 int p[50]; 3 4 //initialize each distance of vertex to source, first source can't be reached 5 //p is the parent of vertex 6 voi
阅读全文
摘要:使用贪心策略,伪代码如下mst1 GENERIC-MST(G,w)2 A=空集3 while(A不形成生成树)4 找到一条安全边5 将此边添加到A中去6 return A在Kruskal算法中,集合A是一个森林,加入集合A中的安全边总是图中连接两个不同连通分支的最小权边。在Prim算法中,集合A仅形成单棵树,添加入集合A的安全边总是连接树与一个不在树中得顶点的最小权边。info 1 typedef struct Edge 2 { 3 int adj_vertex; 4 int weigh...
阅读全文
浙公网安备 33010602011771号