標籤:linux、c
資料是穩定的(即不允許插入操作和刪除操作)
在任意時刻,演算法都能對它已經讀入的資料給出子序列問題的答案,具有這種特性的演算法叫做聯機演算法(online algorithm)
分治(divide-and-conquer)策略:其想法是把問題分成兩個大致相等的子問題,然後遞迴地對他們求解,這是“分”部分。“治”階段將兩個子問題的解合并到一起並可能再做些少量的附加工作,最後得到整個問題的解。
當編寫遞迴常式的時候,關鍵是要牢記遞迴地四條基本法則:
基準情形。 某些基準情形,無須遞迴就能解出
不斷推進。 每次遞迴都必須使求解狀況朝基準情形推進
設計法則。 假設所有的遞迴調用都能運行
合成效益法則(compound interest rule)。在求解一個問題的同一個執行個體時,切勿在不同的遞迴調用中做重複性的工作
如果一個演算法用常數時間(O(1))將問題的大小削減為其一部分(通常是1/2),那麼該演算法就是O(logN)。 另一方面,如果使用常數時間只是把問題減少一個常數(如將問題減少1),那麼這種演算法就是O(N)的。
程式1:輸出數組中第K大的數
#include<stdio.h>#define SIZE 100int main(){ int i,j,k,t,n,num[SIZE],tmp[SIZE]; //the original array num as follows printf("please input the array size(n) : "); scanf("%d",&n); printf("please input the array(including %d integer) :\n",n); for(i=0;i<n;++i) { scanf("%d",&num[i]); } //output the kth number in num printf("please input the number k(rank k‘th in the descending order array):"); scanf("%d",&k); tmp[0]=num[0]; //put the number num[i] into tmp[t],in tmp,the order is descending for(i=1;i<k;++i) { for(j=i-1;j>=0;--j) { if(num[i]<=tmp[j]) { break; } else { tmp[j+1]=tmp[j]; } } t=j+1; tmp[t]=num[i]; } //insert num[i] into tmp[j+1] when num[i]>tmp[k] for(i=k;i<n;++i) { for(j=k-1;j>=0;--j) { if(num[i]<=tmp[j]) break; else tmp[j+1]=tmp[j]; } if(j+1<=k-1) { tmp[j+1]=num[i]; } } printf("Following the descending order,the %dth number is : %d\n",k,tmp[k-1]); return 0;}
程式2:輸出檔案中的全部資料
#include<stdio.h>#include<stdlib.h>int main(){ FILE *fp=fopen("/usr/include/stdio.h","r+"); char* buffer=malloc(sizeof(char)*65535); if(fp==NULL) { printf("fp is NULL"); return 0; } while(fread(buffer,sizeof(char),65535,fp)!=0) { printf("%s",buffer); } printf("\n"); fclose(fp); return 0;}
程式3:輸出最大子序列和。以下3個演算法,資料量很大時,已耗用時間遞減
演算法1:
#include<stdio.h>#define SIZE 10int main(){ int num[SIZE]; int i,j; int tmp,sum=0; int start=0,end=0; printf("please input the element of the array[%d] :\n",SIZE); for(i=0;i<SIZE;++i) scanf("%d",&num[i]); for(i=0;i<SIZE;++i) { tmp=0; for(j=i;j<SIZE;++j) { tmp+=num[j]; if(sum<tmp) { start=i; sum=tmp; end=j; } } } printf("the max_sublist_sum is %d\n",sum); for(i=start;i<end;++i) printf("%d ",num[i]); printf("%d\n",num[end]); return 0;}
演算法2:
#include<stdio.h>#define SIZE 10int maxsubsum(int array[],int left,int right){ int maxleftsum,maxrightsum,maxcentersum; int maxleftbordersum=0,maxrightbordersum=0; int leftbordersum=0,rightbordersum=0; int center,i; if(left==right) if(array[left]>0) return array[left]; else return 0; center=(left+right)/2; maxleftsum=maxsubsum(array,left,center); maxrightsum=maxsubsum(array,center+1,right); for(i=center;i>=left;--i) { leftbordersum+=array[i]; if(maxleftbordersum<leftbordersum) maxleftbordersum=leftbordersum; } for(i=center+1;i<=right;++i) { rightbordersum+=array[i]; if(maxrightbordersum<rightbordersum) maxrightbordersum=rightbordersum; } maxcentersum=maxrightbordersum+maxleftbordersum; return maxleftsum>maxrightsum?(maxleftsum>maxcentersum?maxleftsum:maxcentersum):(maxrightsum>maxcentersum?maxrightsum:maxcentersum); }int main(){ int array[SIZE],left=0,right=SIZE-1,i; printf("please input the element of the array[%d] :\n",SIZE); for(i=0;i<SIZE;++i) { scanf("%d",&array[i]); } printf("the max_sublist_sum is : %d\n",maxsubsum(array,left,right)); /* for(i=left;i<right;++i) printf("%d ",array[i]); printf("%d\n",array[right]); */ return 0;}
演算法3:
#include<stdio.h>#define SIZE 10int main(){ int thissum=0,maxsum=0,i; int array[SIZE]; printf("please input the element of the array[%d] :\n",SIZE); for(i=0;i<SIZE;++i) { scanf("%d",&array[i]); } for(i=0;i<SIZE;++i) { thissum+=array[i]; if(thissum>maxsum) maxsum=thissum; else if(thissum<0) thissum=0; } printf("the max_sublist_sum is : %d\n",maxsum); return 0;}
程式4:對分尋找(binary search)
#include<stdio.h>#define SIZE 10int main(){ int array[SIZE],i,goal,left=0,right=SIZE-1,mid; printf("please input the element of the array[%d]:\n",SIZE); for(i=0;i<SIZE;++i) { scanf("%d",&array[i]); } printf("please input the goal :"); scanf("%d",&goal); while(left<=right) { mid=(left+right)/2; if(array[mid]>goal) right=mid-1; else if(array[mid]<goal) left=mid+1; else { printf("the index is : %d\n",mid); return 0; } } printf("not find the goal\n"); return 0;}
程式5:計算最大公因數的歐幾裡得演算法
#include<stdio.h>Gcd(unsigned int M,unsigned int N){ unsigned int Rem; while(N>0) { Rem=M%N; M=N; N=Rem; } return M;}int main(){ unsigned int M,N; printf("please input M and N :"); scanf("%d %d",&M,&N); printf("the Gcd is : %d\n",Gcd(M,N)); return 0;}
程式6:冪運算
#include<stdio.h>long int Pow(long int x,unsigned int N){ long int tmp; if(N==0) return 1; else if(N==1) return x; tmp=Pow(x*x,N/2); if(N%2!=0) tmp*=x; return tmp; }int main(){ printf("please input the x and N: "); long int x; unsigned int N; scanf("%ld %u",&x,&N); printf("the result is :%ld\n",Pow(x,N)); return 0;}
程式7:計算兩個隨機選取出並小於或等於NN的互異正整數互素的機率
#include<stdio.h>#define NN 6unsigned int Gcd(unsigned int M,unsigned int N){ unsigned int Rem; while(N>0) { Rem=M%N; M=N; N=Rem; } return M;}int main(){ int Rel=0,Tot=0,j,i; for(i=1;i<=NN;++i) for(j=i+1;j<=NN;++j) { Tot++; if(Gcd(i,j)==1) Rel++; } printf("the probability of it that they are prime is :%f\n",(Rel+0.0)/Tot); return 0;}
《資料結構與演算法分析——c語言描述》讀後筆記