|
| 1 | +/* ************************************************************************ |
| 2 | +> File Name: section_5.cpp |
| 3 | +> Author: sunxiuyang |
| 4 | + |
| 5 | +> Created Time: Mon Feb 24 18:44:56 2020 |
| 6 | +> Description: |
| 7 | + ************************************************************************/ |
| 8 | + |
| 9 | +#include <iostream> |
| 10 | +#include <chrono> |
| 11 | +#include <thread> |
| 12 | +using namespace std; |
| 13 | +using namespace chrono; |
| 14 | + |
| 15 | +int fibonacci_1(int n) { |
| 16 | + if (n <= 0) { |
| 17 | + return 0; |
| 18 | + } |
| 19 | + if (n == 1 || n == 2) { |
| 20 | + return 1; |
| 21 | + } |
| 22 | + int first = 1, second = 1; |
| 23 | + int sum = first + second; |
| 24 | + for (int i = 2; i < n; i++) { |
| 25 | + sum = first + second; |
| 26 | + first = second; |
| 27 | + second = sum; |
| 28 | + } |
| 29 | + return sum; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +int fibonacci_2(int i) { |
| 34 | + if(i <= 0) return 0; |
| 35 | + if(i == 1) return 1; |
| 36 | + return fibonacci_2(i - 1) + fibonacci_2(i - 2); |
| 37 | +} |
| 38 | + |
| 39 | +int fibonacci_3(int first, int second, int n) { |
| 40 | + if (n <= 0) { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + if (n < 3) { |
| 44 | + return 1; |
| 45 | + } |
| 46 | + else if (n == 3) { |
| 47 | + return first + second; |
| 48 | + } |
| 49 | + else { |
| 50 | + return fibonacci_3(second, first + second, n - 1); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +void test_fibonacci() { |
| 56 | + int n; |
| 57 | + while(cin >> n) { |
| 58 | + cout << fibonacci_1(n) << endl; |
| 59 | + cout << fibonacci_2(n) << endl; |
| 60 | + cout << fibonacci_3(1, 1, n) << endl; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +int binary_search(int arr[], int l, int r, int n) { |
| 65 | + if (r >= l) { |
| 66 | + int mid = l + (r - l) / 2; |
| 67 | + if (arr[mid] == n) |
| 68 | + return mid; |
| 69 | + if (arr[mid] > n) |
| 70 | + return binary_search(arr, l, mid - 1, n); |
| 71 | + return binary_search(arr, mid + 1, r, n); |
| 72 | + } |
| 73 | + return -1; |
| 74 | +} |
| 75 | +int arr[] = {2, 3, 4, 5, 8, 10, 15, 17, 20}; |
| 76 | +int binary_search(int l, int r, int n) { |
| 77 | + if (r >= l) { |
| 78 | + int mid = l + (r - l) / 2; |
| 79 | + if (arr[mid] == n) |
| 80 | + return mid; |
| 81 | + if (arr[mid] > n) |
| 82 | + return binary_search(l, mid - 1, n); |
| 83 | + return binary_search(mid + 1, r, n); |
| 84 | + } |
| 85 | + return -1; |
| 86 | +} |
| 87 | + |
| 88 | +void test_binary_search(void) { |
| 89 | + int arr[] = {2, 3, 4, 5, 8, 10, 15, 17, 20}; |
| 90 | + int x = 17; |
| 91 | + int n = sizeof(arr) / sizeof(arr[0]); |
| 92 | + // int result = binary_search(arr, 0, n - 1, x); |
| 93 | + int result = binary_search(0, n - 1, x); |
| 94 | + (result == -1) ? cout << "Element is not present in array" |
| 95 | + : cout << "Element is present at index " << result; |
| 96 | +} |
| 97 | + |
| 98 | +int main() |
| 99 | +{ |
| 100 | + // test_binary_search(); |
| 101 | + // test_fibonacci(); |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
0 commit comments