# 印星星
印星星 | #include <iostream> |
| using namespace std; |
| |
| void printUpperTriangle(int n) { |
| for (int i = 0; i < n; i++) { |
| for (int j = 0; j <= i; j++) { |
| cout << "* "; |
| } |
| cout << endl; |
| } |
| } |
| |
| void printLowerTriangle(int n) { |
| for (int i = n; i > 0; i--) { |
| for (int j = 0; j < i; j++) { |
| cout << "* "; |
| } |
| cout << endl; |
| } |
| } |
| |
| int main() { |
| int n; |
| cout << "請輸入行數: "; |
| cin >> n; |
| |
| cout << "上三角形:" << endl; |
| printUpperTriangle(n); |
| |
| cout << "下三角形:" << endl; |
| printLowerTriangle(n); |
| |
| return 0; |
| } |
# 用遞迴寫階乘
用遞迴寫階乘 | #include <iostream> |
| using namespace std; |
| |
| |
| int factorial(int n) { |
| |
| if (n == 0 || n == 1) { |
| return 1; |
| } |
| |
| |
| return n * factorial(n - 1); |
| } |
| |
| int main() { |
| int num; |
| cout << "請輸入一個正整數: "; |
| cin >> num; |
| |
| |
| int result = factorial(num); |
| |
| cout << num << " 的階乘是: " << result << endl; |
| |
| return 0; |
| } |
# 找出陣列中第二大的數字
找出陣列中第二大的數字 | #include <iostream> |
| using namespace std; |
| |
| int findSecondLargest(int arr[], int size) { |
| int largest = INT_MIN; |
| int secondLargest = INT_MIN; |
| |
| |
| for (int i = 0; i < size; i++) { |
| if (arr[i] > largest) { |
| secondLargest = largest; |
| largest = arr[i]; |
| } else if (arr[i] > secondLargest && arr[i] != largest) { |
| secondLargest = arr[i]; |
| } |
| } |
| |
| return secondLargest; |
| } |
| |
| int main() { |
| int arr[] = {5, 2, 9, 1, 7, 4}; |
| int size = sizeof(arr) / sizeof(arr[0]); |
| |
| |
| int secondLargest = findSecondLargest(arr, size); |
| |
| cout << "第二大的數字是: " << secondLargest << endl; |
| |
| return 0; |
| } |
# 給定一數字,找出因數
給定一數字,找出因數 | #include <iostream> |
| #include <vector> |
| |
| using namespace std; |
| |
| |
| vector<int> findFactors(int num) { |
| vector<int> factors; |
| |
| |
| for (int i = 1; i * i <= num; i++) { |
| if (num % i == 0) { |
| factors.push_back(i); |
| if (i != num / i) { |
| factors.push_back(num / i); |
| } |
| } |
| } |
| |
| return factors; |
| } |
| |
| int main() { |
| int num; |
| cout << "請輸入一個正整數:"; |
| cin >> num; |
| |
| vector<int> factors = findFactors(num); |
| |
| cout << num << " 的因數有:"; |
| for (int factor : factors) { |
| cout << factor << " "; |
| } |
| cout << endl; |
| |
| return 0; |
| } |
# 判斷質數
判斷質數 | #include <iostream> |
| #include <cmath> |
| |
| using namespace std; |
| |
| |
| bool isPrime(int number) { |
| |
| if (number <= 1) { |
| return false; |
| } |
| |
| |
| |
| |
| |
| int sqrtNumber = sqrt(number); |
| for (int i = 2; i <= sqrtNumber; i++) { |
| if (number % i == 0) { |
| return false; |
| } |
| } |
| |
| |
| return true; |
| } |
| |
| |
| int main() { |
| int number; |
| |
| cout << "請輸入一個數字:"; |
| cin >> number; |
| |
| |
| bool result = isPrime(number); |
| |
| |
| if (result) { |
| cout << number << " 是質數" << endl; |
| } else { |
| cout << number << " 不是質數" << endl; |
| } |
| |
| return 0; |
| } |
# 判斷子字串
判斷子字串 | #include <iostream> |
| #include <string> |
| |
| using namespace std; |
| |
| |
| bool isSubstring(const string& str, const string& substring) { |
| |
| if (substring.length() > str.length()) { |
| return false; |
| } |
| |
| |
| for (size_t i = 0; i <= str.length() - substring.length(); i++) { |
| bool isMatch = true; |
| |
| |
| for (size_t j = 0; j < substring.length(); j++) { |
| if (str[i + j] != substring[j]) { |
| isMatch = false; |
| break; |
| } |
| } |
| |
| |
| if (isMatch) { |
| return true; |
| } |
| } |
| |
| |
| return false; |
| } |
| |
| |
| int main() { |
| string str, substring; |
| |
| cout << "請輸入一個字串:"; |
| getline(cin, str); |
| |
| cout << "請輸入一個子字串:"; |
| getline(cin, substring); |
| |
| |
| bool result = isSubstring(str, substring); |
| |
| |
| if (result) { |
| cout << substring << " 是 " << str << "的子字串" << endl; |
| } else { |
| cout << substring << " 不是 " << str <<" 的子字串 " << endl; |
| } |
| |
| return 0; |
| } |
-
檢查子字串的長度是否大於原始字串的長度,如果是,則直接返回 false,因為子字串不可能是原始字串的子字串。
-
使用兩個嵌套的迴圈。外層迴圈遍歷原始字串,內層迴圈檢查從當前位置開始的子字串是否匹配。如果在內層迴圈中找到了不匹配的字元,則設置 isMatch
為 false,並且跳出內層迴圈。
# Binary Search Tree
二元搜尋樹 | #include <iostream> |
| |
| using namespace std; |
| |
| |
| struct Node { |
| int data; |
| Node* left; |
| Node* right; |
| |
| |
| Node(int value) { |
| data = value; |
| left = nullptr; |
| right = nullptr; |
| } |
| }; |
| |
| |
| bool search(Node* root, int value) { |
| |
| if (root == nullptr) { |
| return false; |
| } |
| |
| |
| if (root->data == value) { |
| return true; |
| } |
| |
| |
| if (value < root->data) { |
| return search(root->left, value); |
| } |
| |
| |
| return search(root->right, value); |
| } |
| |
| |
| Node* insert(Node* root, int value) { |
| |
| if (root == nullptr) { |
| return new Node(value); |
| } |
| |
| |
| if (value < root->data) { |
| root->left = insert(root->left, value); |
| } |
| |
| |
| else if (value > root->data) { |
| root->right = insert(root->right, value); |
| } |
| |
| |
| |
| return root; |
| } |
| |
| |
| Node* remove(Node* root, int value) { |
| |
| if (root == nullptr) { |
| return root; |
| } |
| |
| |
| if (value < root->data) { |
| root->left = remove(root->left, value); |
| } |
| |
| else if (value > root->data) { |
| root->right = remove(root->right, value); |
| } |
| |
| else { |
| |
| if (root->left == nullptr) { |
| Node* temp = root->right; |
| delete root; |
| return temp; |
| } |
| else if (root->right == nullptr) { |
| Node* temp = root->left; |
| delete root; |
| return temp; |
| } |
| |
| |
| |
| Node* minNode = root->right; |
| while (minNode->left != nullptr) { |
| minNode = minNode->left; |
| } |
| |
| root->data = minNode->data; |
| |
| root->right = remove(root->right, minNode->data); |
| } |
| |
| |
| return root; |
| } |
| |
| |
| int main() { |
| |
| Node* root = nullptr; |
| root = insert(root, 50); |
| root = insert(root, 30); |
| root = insert(root, 20); |
| root = insert(root, 40); |
| root = insert(root, 70); |
| root = insert(root, 60); |
| root = insert(root, 80); |
| |
| |
| if (search(root, 40)) { |
| cout << "搜尋到值 40" << endl; |
| } else { |
| cout << "未找到值 40" << endl; |
| } |
| |
| |
| root = remove(root, 30); |
| |
| |
| if (search(root, 30)) { |
| cout << "搜尋到值 30" << endl; |
| } else { |
| cout << "未找到值 30" << endl; |
| } |
| |
| return 0; |
| } |
我們使用 struct
定義了二元搜尋樹的節點結構,每個節點包含一個整數數據、左子樹指針和右子樹指針。
-
search
函式實現了搜尋操作。它遞迴地在二元搜尋樹中搜尋目標值。如果樹為空則返回 false
,如果找到了目標值則返回 true
,否則根據目標值與根節點數據的比較結果遞迴地在左子樹或右子樹中搜尋。
-
insert
函式實現了插入操作。它遞迴地在二元搜尋樹中找到合適的位置插入新節點。如果樹為空則創建一個新節點,否則根據目標值與根節點數據的比較結果遞迴地在左子樹或右子樹中插入。如果目標值等於根節點的值,則不進行插入操作(忽略重複值)。
-
remove
函式實現了刪除操作。它遞迴地在二元搜尋樹中找到目標值所在的節點,並根據不同情況進行刪除。若目標節點沒有子節點或只有一個子節點,則直接刪除該節點並返回相應的子節點。若目標節點有兩個子節點,則找到右子樹中的最小值節點,將最小值複製到目標節點,然後在右子樹中刪除最小值節點。
在 main
函式中,插入了一些節點,然後使用 search
函式搜尋值 40,並根據結果輸出相應的訊息。
接著,我們刪除值 30 的節點,再次使用 search
函式搜尋值 30,並根據結果輸出相應的訊息。