標籤:++ dfa false array 重要 原則 dom can 演算法思想
在平時的工作中遇到純粹的演算法設計的工作內容並不多,但是演算法在編程中的重要性是不言而喻的,再怎麼拔高演算法的地位都不為過。
那麼在設計演算法中有什麼可以遵循的原則嗎?
答案是有的,演算法在設計的過程中可以遵循如下五個原則。
1.窮舉演算法思想
窮舉演算法思想就是從所有的可能結果中一個一個的實驗,知道試出正確的結果。具體的操作步驟如下:
1)對每一種可能的結果,計算其結果;
2)判斷結果是否符合題目要求,如果符合則該結果正確,如果不符合則繼續進行第1)步驟。
窮舉演算法思想的經典例子為雞兔同籠為題(又稱龜鶴同籠問題),題目為“一個籠子裡有雞兔,共15個頭、46條腿,問雞兔各有多少只?”。代碼如下:
public static void main(String[] args) {
int head = 0;
int leg = 0;
System.out.println( "輸入雞兔頭數:");
Scanner input=new Scanner(System.in);
head = input.nextInt();
System.out.println( "輸入雞兔腿數:");
Scanner input1=new Scanner(System.in);
leg = input1.nextInt();
boolean existence = false;
for( int i = 0; i <= head; i++){
if( 2 * i + 4 * ( head - i) == leg){
System.out.println( "雞的個數 :" + i);
System.out.println( "兔的個數 :" + ( head - i));
existence = true;
}
}
if( !existence){
System.out.println( "你輸入的資料不正確");
}
}
2.遞推演算法思想
遞推演算法演算法就是根據已知條件,利用特定關係推匯出中間推論,直到得到結果的演算法。
遞推演算法思想最經典的例子是斐波那契數列 : 1,1,2,3,5,8,13......
上面的數列符合F(n) = F(n-1) + F(n-2).代碼如下:
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n = input.nextInt();
System.out.println( fibonacci( n));
}
public static int fibonacci( int n){
if( n == 1){
return 1;
}else if( n == 2){
return 1;
}else{
return fibonacci( n - 1) + fibonacci( n - 2);
}
}
3.遞迴演算法思想
遞迴演算法思想是把大問題轉換成同類問題的子問題,然後遞迴調用函數表示問題的解。
在使用遞迴的時候一定要注意調回遞迴函式的終止條件。
遞迴演算法比較經典的例子是求階乘。代碼如下:
public static void main(String[] args) {
System.out.println( "輸入一個大於零的數:");
Scanner input=new Scanner(System.in);
int n = input.nextInt();
System.out.println( factorial( n));
}
public static int factorial( int n){
if( n == 0){
return 1;
}else if( n == 1){
return 1;
}else{
return ( n * factorial( n-1));
}
}
4.分治演算法思想
分治演算法思想就是把一個大問題分解成若干個規模較小的子問題,且這些子問題的都是相互獨立的、與原問題性質一致。逐個求出這些子問題的解就能夠得到原問題的解了。
分治演算法思想有一個比較經典的例子就是尋找假幣問題。
題目描述:現有35枚硬幣,其中只有一枚是假幣。已知假幣與真幣外表完全一樣,只是比真幣輕一點。請找出假幣。代碼如下:
public static void main(String[] args) {
int index = 0;
System.out.println( "輸入金幣總數:");
Scanner input = new Scanner( System.in);
index = input.nextInt();
int[] arrayInt = new int[ index];
for( int i=0; i<index; i++){
arrayInt[i] = 2;
}
int falseCoin = (int)( Math.random() * index);
System.out.println( "假幣:" + falseCoin);
arrayInt[ falseCoin] = 1;
System.out.println( "假幣是:" + findFalseCoin( arrayInt, 0, index-1));
}
public static int findFalseCoin( int[] arrayInt, int low, int high){
int tmp = 0;
if( low + 1 == high){
if( arrayInt[low] > arrayInt[high]){
return high;
}else{
return low;
}
}
tmp = high - low + 1;
if( tmp % 2 == 0){
int tmp1 = totalWeight( arrayInt, low, low + tmp/2 -1);
int tmp2 = totalWeight( arrayInt, low + tmp/2, high);
if( tmp1 > tmp2){
return findFalseCoin( arrayInt, low + tmp/2, high);
}else{
return findFalseCoin( arrayInt, low, low + tmp/2 -1);
}
}else{
int tmp1 = totalWeight( arrayInt, low, low + tmp/2 -1);
int tmp2 = totalWeight( arrayInt, low + tmp/2 +1, high);
if( tmp1 > tmp2){
return findFalseCoin( arrayInt, low + tmp/2 +1, high);
}else if( tmp1 < tmp2){
return findFalseCoin( arrayInt, low, low + tmp/2 -1);
}else{
return low + tmp/2;
}
}
}
public static int totalWeight( int[] arrayInt, int low, int high){
int sum = 0;
for( int i = low; i <= high; i++){
sum += arrayInt[ i];
}
return sum;
}
5.機率演算法思想
51.數值機率演算法
5.2蒙特卡羅演算法
5.3拉斯維加斯演算法
5.4舍伍德演算法
這個演算法思想還沒有理解,歡迎大家補充。
演算法設計原則