以爲仍是先從簡單例子程序着手,先了解各個算法的基本思想。html
目錄:ios
算法是指對特定問題求解步驟 的一種描述c++
算法的評價標準:時間複雜度與空間複雜度。算法
時間複雜度:考慮給定的數據數目n,關於算法的執行次數。漸進上界用O()表示,最壞狀況對衡量算法的好壞具備實際的意義。數組
空間複雜度:算法佔用的空間大小。通常將算法的輔助空間做爲衡量標準。網絡
算法思想:一個貪心算法老是作出當前最好的選擇,也就是說,它指望經過局部最優選擇從而獲得全局最優的解決方案。函數
小插曲:c++中有排序函數sort,能夠直接利用spa
#include<iostream> using namespace std; #include<algorithm> int main() { int a[4] = { 4,7,3,5 }; for (int i = 0; i < 4; i++) cout << a[i] << " "; cout << endl << "排序後::::" << endl; sort(a, a + 3); //能夠只對某幾項進行排列 for (int i = 0; i < 4; i++) cout << a[i] << " "; cin.ignore(); return 0; }
4 7 3 5
排序後::::
3 4 7 5設計
舉列1:最優裝載問題
描述:海盜們截獲一批財寶,海盜船載重有限,如何儘量多的獲取寶貝??
算法設計:每次選擇重量最小的,直到不能再裝爲止。
代碼:
#include<iostream> using namespace std; #include<algorithm> const int N = 100; double w[N]; //古董 的重量數組 int main() { double c; int n; cout << "請輸入載重量c以及古董個數n:" << endl; cin >> c >> n; cout << "請輸入每一個古董的重量" << endl; for (int i = 0; i < n; i++) { cin >> w[i]; } sort(w, w + n); double temp = 0; int ans = 0; for (int i = 0; i < n; i++) { temp += w[i]; if (temp <= c) ans++; else break; } cout << "最大裝載數目:" << ans << endl; cin.ignore(); cin.ignore(); cin.ignore(); return 0; }
請輸入載重量c以及古董個數n:
30 8
請輸入每一個古董的重量
4 10 7 11 3 5 14 2
最大裝載數目:5
擴展:如何知道裝入了那些寶貝呢??
引入結構體數組,增長寶貝的編號屬性,具體程序以下
1 #include<iostream> 2 using namespace std; 3 #include<algorithm> 4 5 const int N = 100; 6 struct two { 7 int n; //寶物標號 8 double w; //寶物重量 9 }s[N]; 10 //double w[N]; //古董 的重量數組 11 bool cmp(two a, two b) 12 { 13 return a.w < b.w; 14 } 15 16 int main() 17 { 18 double c; 19 int n; 20 cout << "請輸入載重量c以及古董個數n:" << endl; 21 cin >> c >> n; 22 23 cout << "請輸入每一個古董的重量" << endl; 24 25 for (int i = 0; i < n; i++) 26 { 27 cin >> s[i].w; 28 s[i].n = i + 1; 29 } 30 sort(s, s + n, cmp); 31 32 double temp = 0; 33 int ans = 0; 34 35 for (int i = 0; i < n; i++) 36 { 37 temp += s[i].w; 38 if (temp <= c) 39 { 40 ans++; 41 cout << "選擇第" << s[i].n << "個寶貝" << endl; 42 } 43 else 44 break; 45 } 46 cout << "最大裝載數目:" << ans << endl; 47 48 49 cin.ignore(); 50 cin.ignore(); 51 cin.ignore(); 52 return 0; 53 }
請輸入載重量c以及古董個數n:
30 8
請輸入每一個古董的重量
4 10 7 11 3 5 14 2
選擇第8個寶貝
選擇第5個寶貝
選擇第1個寶貝
選擇第6個寶貝
選擇第3個寶貝
最大裝載數目:5
算法思想:本質是將一個大規模的問題分解爲若干個規模較小 的相同子問題,分而治之
在分治算法中,各個子問題形式相同,解決的方法也同樣,所以可使用遞歸算法快速解決,遞歸是彰顯分治法優點的利器
舉列1:猜數遊戲
描述:n個數,給定一個數x,從中找出特定的元素x
算法設計:每次從中間元素開始 查找,與x進行比較,這樣每次就能夠將問題的規模減半,直到查到x
code:
1 #include<iostream> 2 3 #include<algorithm> 4 using namespace std; 5 6 const int M = 10000; 7 int s[M]; 8 9 int BinarySearch(int n, int s[], int x) 10 { 11 int low = 0, high = n - 1; 12 while (low <= high) 13 { 14 int middle = (low + high) / 2; 15 if (x == s[middle]) 16 return middle; 17 else if (x < s[middle]) 18 high = middle - 1; 19 else 20 low = middle + 1; 21 } 22 return -1; 23 } 24 25 int main() 26 { 27 int n,x,i; 28 29 cout << "請輸入數列中的元素個數n爲: "; 30 while (cin >> n) 31 { 32 cout << "請依次輸入:"; 33 for (int i = 0; i < n; i++) 34 cin >> s[i]; 35 sort(s, s + n); 36 cout << "排序後的數組爲:"; 37 for (int i = 0; i < n; i++) 38 cout << s[i]<<" "; 39 cout << endl; 40 cout << "輸入要查找的元素:"; 41 cin >> x; 42 i = BinarySearch(n, s, x); 43 if (i == -1) 44 cout << "no found"; 45 else 46 cout << "the element found is" << i + 1 << "位" << endl; 47 48 49 } 50 51 cin.ignore(); 52 return 0; 53 }
請輸入數列中的元素個數n爲: 11
請依次輸入:2 6 3 7 99 33 45 82 14 32 5
排序後的數組爲:2 3 5 6 7 14 32 33 45 82 99
輸入要查找的元素:3
the element found is2位
擴展:使用遞歸算法實現:
1 #include<iostream> 2 3 #include<algorithm> 4 using namespace std; 5 6 const int M = 10000; 7 int s[M]; 8 9 int BinarySearch(int s[], int x, int low, int high) 10 { 11 12 if (low>high) 13 return -1; 14 int middle = (low + high) / 2; 15 if (x == s[middle]) 16 return middle; 17 else if (x < s[middle]) 18 return BinarySearch(s, x, low, middle - 1); 19 else 20 return BinarySearch(s, x, middle + 1, high); 21 } 22 23 int main() 24 { 25 int n,x,i; 26 27 cout << "請輸入數列中的元素個數n爲: "; 28 while (cin >> n) 29 { 30 cout << "請依次輸入:"; 31 for (int i = 0; i < n; i++) 32 cin >> s[i]; 33 sort(s, s + n); 34 cout << "排序後的數組爲:"; 35 for (int i = 0; i < n; i++) 36 cout << s[i]<<" "; 37 cout << endl; 38 cout << "輸入要查找的元素:"; 39 cin >> x; 40 i = BinarySearch(s,x,0,n-1); 41 if (i == -1) 42 cout << "no found"; 43 else 44 cout << "the element found is" << i + 1 << "位" << endl; 45 46 47 } 48 49 cin.ignore(); 50 return 0; 51 }
算法思想: