1.折半查找
#include<stdio.h> int binary_search(int arr[], int k, int left, int right) { while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == k) { return mid; } else if (arr[mid] < k) { left = mid + 1; } else { right = mid - 1; } } return -1; } int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int key = 9; int sz = sizeof(arr) / sizeof(arr[0]); int ret = binary_search(arr, key, 1, 10); if (-1 == ret) { printf("找不到\n"); } else { printf("找到了,下标是%d\n", ret); } system("pause"); return 0; }
2.大小写转换
# include<stdio.h> # include<stdlib.h> int main() { int ch = 0; while ((ch = getchar()) != EOF) { if (ch >= 'A' && ch <= 'Z') { putchar(ch + 32); } else if (ch >= 'a' && ch <= 'z') { putchar(ch - 32); } } return 0; }