1. Hello World

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

2. 两数相加

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "输入两个整数(空格分隔): ";
    cin >> a >> b;
    cout << "和: " << a + b << endl;
    return 0;
}

3. 比较大小

#include <iostream>
using namespace std;

int main() {
    int x, y;
    cout << "输入两个数: ";
    cin >> x >> y;
    cout << "较大值: " << (x > y ? x : y) << endl;
    return 0;
}

4. 判断奇偶

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "输入整数: ";
    cin >> num;
    cout << num << "是" << (num % 2 == 0 ? "偶数" : "奇数") << endl;
    return 0;
}

5. 成绩等级

#include <iostream>
using namespace std;

int main() {
    int score;
    cout << "输入分数(0-100): ";
    cin >> score;
    
    if (score >= 90) cout << "A";
    else if (score >= 80) cout << "B";
    else if (score >= 70) cout << "C";
    else if (score >= 60) cout << "D";
    else cout << "F";
    
    cout << endl;
    return 0;
}

6. 计算阶乘

#include <iostream>
using namespace std;

int main() {
    int n;
    long long fact = 1; // 避免溢出
    cout << "输入非负整数: ";
    cin >> n;
    
    for (int i = 2; i <= n; i++) {
        fact *= i;
    }
    cout << n << "! = " << fact << endl;
    return 0;
}

7. 判断素数

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int num) {
    if (num <= 1) return false;
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) return false;
    }
    return true;
}

int main() {
    int num;
    cout << "输入整数: ";
    cin >> num;
    cout << num << (isPrime(num) ? "是素数" : "不是素数") << endl;
    return 0;
}

8. 斐波那契数列

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "输入项数: ";
    cin >> n;
    
    int a = 0, b = 1;
    cout << "斐波那契数列: ";
    for (int i = 0; i < n; i++) {
        cout << a << " ";
        int next = a + b;
        a = b;
        b = next;
    }
    return 0;
}

9. 九九乘法表

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= i; j++) {
            cout << j << "×" << i << "=" << i*j << "\t";
        }
        cout << endl;
    }
    return 0;
}

10. 求和(1+2+…+n)

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    cout << "输入正整数n: ";
    cin >> n;
    
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    cout << "和: " << sum << endl;
    return 0;
}

11. 求平均数

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<double> nums;
    double num, sum = 0;
    
    cout << "输入数字(输入任意字母结束): ";
    while (cin >> num) {
        nums.push_back(num);
        sum += num;
    }
    
    if (!nums.empty()) {
        cout << "平均数: " << sum / nums.size() << endl;
    } else {
        cout << "未输入有效数据!" << endl;
    }
    return 0;
}

12. 反转数字

#include <iostream>
using namespace std;

int reverseNumber(int n) {
    int reversed = 0;
    while (n != 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }
    return reversed;
}

int main() {
    int num;
    cout << "输入整数: ";
    cin >> num;
    cout << "反转后: " << reverseNumber(num) << endl;
    return 0;
}

13. 进制转换(十进制转二进制)

#include <iostream>
#include <bitset>
using namespace std;

void decimalToBinary(int n) {
    if (n == 0) {
        cout << "0";
        return;
    }
    
    string binary;
    while (n > 0) {
        binary = to_string(n % 2) + binary;
        n /= 2;
    }
    cout << binary;
}

int main() {
    int num;
    cout << "输入十进制数: ";
    cin >> num;
    cout << "二进制: ";
    decimalToBinary(num);
    cout << endl;
    
    // 使用STL的bitset(仅适用于无符号数)
    // cout << "STL实现: " << bitset<32>(num) << endl;
    return 0;
}

14. 最大公约数(GCD)

#include <iostream>
using namespace std;

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int a, b;
    cout << "输入两个整数: ";
    cin >> a >> b;
    cout << "GCD: " << gcd(a, b) << endl;
    return 0;
}

15. 最小公倍数(LCM)

#include <iostream>
using namespace std;

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

int lcm(int a, int b) {
    return a * b / gcd(a, b);
}

int main() {
    int a, b;
    cout << "输入两个整数: ";
    cin >> a >> b;
    cout << "LCM: " << lcm(a, b) << endl;
    return 0;
}

16. 求解一元二次方程

#include <iostream>
#include <cmath>
using namespace std;

void solveQuadratic(double a, double b, double c) {
    double discriminant = b * b - 4 * a * c;
    
    if (discriminant > 0) {
        double root1 = (-b + sqrt(discriminant)) / (2 * a);
        double root2 = (-b - sqrt(discriminant)) / (2 * a);
        cout << "实根: " << root1 << " 和 " << root2 << endl;
    } else if (discriminant == 0) {
        double root = -b / (2 * a);
        cout << "重根: " << root << endl;
    } else {
        cout << "无实根(存在复数根)" << endl;
    }
}

int main() {
    double a, b, c;
    cout << "输入系数a, b, c(ax²+bx+c=0): ";
    cin >> a >> b >> c;
    
    if (a == 0) {
        cout << "不是二次方程!" << endl;
    } else {
        solveQuadratic(a, b, c);
    }
    return 0;
}

17. 猜数字游戏

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0));
    int secret = rand() % 100 + 1;
    int guess, attempts = 0;
    
    cout << "猜数字游戏(1-100)" << endl;
    do {
        cout << "你的猜测: ";
        cin >> guess;
        attempts++;
        
        if (guess > secret) cout << "太大了!" << endl;
        else if (guess < secret) cout << "太小了!" << endl;
    } while (guess != secret);
    
    cout << "恭喜!用了 " << attempts << " 次猜中。" << endl;
    return 0;
}

18. 简易计算器

#include <iostream>
using namespace std;

int main() {
    char op;
    double num1, num2;
    
    cout << "输入运算符(+ - * /): ";
    cin >> op;
    cout << "输入两个操作数: ";
    cin >> num1 >> num2;
    
    switch (op) {
        case '+':
            cout << "结果: " << num1 + num2 << endl;
            break;
        case '-':
            cout << "结果: " << num1 - num2 << endl;
            break;
        case '*':
            cout << "结果: " << num1 * num2 << endl;
            break;
        case '/':
            if (num2 != 0) {
                cout << "结果: " << num1 / num2 << endl;
            } else {
                cout << "错误:除数不能为0!" << endl;
            }
            break;
        default:
            cout << "无效运算符!" << endl;
    }
    return 0;
}

19. 判断闰年

#include <iostream>
using namespace std;

bool isLeapYear(int year) {
    return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
}

int main() {
    int year;
    cout << "输入年份: ";
    cin >> year;
    cout << year << (isLeapYear(year) ? "是闰年" : "不是闰年") << endl;
    return 0;
}

20. 打印金字塔图案

#include <iostream>
using namespace std;

void printPyramid(int height) {
    for (int i = 1; i <= height; i++) {
        // 打印空格
        for (int j = 1; j <= height - i; j++) {
            cout << " ";
        }
        // 打印星号
        for (int k = 1; k <= 2 * i - 1; k++) {
            cout << "*";
        }
        cout << endl;
    }
}

int main() {
    int rows;
    cout << "输入金字塔行数: ";
    cin >> rows;
    printPyramid(rows);
    return 0;
}

21. 数组最大值

#include <iostream>
#include <climits> // 用于INT_MIN
using namespace std;

int findMax(int arr[], int size) {
    int maxVal = INT_MIN;
    for (int i = 0; i < size; i++) {
        if (arr[i] > maxVal) maxVal = arr[i];
    }
    return maxVal;
}

int main() {
    int arr[] = {12, 45, 67, 23, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    cout << "数组最大值: " << findMax(arr, size) << endl;
    return 0;
}

22. 数组求和

#include <iostream>
using namespace std;

int arraySum(int arr[], int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
    return sum;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    cout << "数组总和: " << arraySum(arr, size) << endl;
    return 0;
}

23. 数组反转

#include <iostream>
using namespace std;

void reverseArray(int arr[], int size) {
    for (int i = 0; i < size / 2; i++) {
        swap(arr[i], arr[size - 1 - i]);
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    cout << "原数组: ";
    for (int i = 0; i < size; i++) cout << arr[i] << " ";
    
    reverseArray(arr, size);
    
    cout << "\n反转后: ";
    for (int i = 0; i < size; i++) cout << arr[i] << " ";
    
    return 0;
}

24. 线性查找

#include <iostream>
using namespace std;

int linearSearch(int arr[], int size, int target) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) return i;
    }
    return -1; // 未找到
}

int main() {
    int arr[] = {12, 45, 67, 23, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target = 67;
    
    int index = linearSearch(arr, size, target);
    if (index != -1) {
        cout << target << " 在数组下标 " << index << " 处" << endl;
    } else {
        cout << "未找到目标值" << endl;
    }
    return 0;
}

25. 冒泡排序

#include <iostream>
using namespace std;

void bubbleSort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    cout << "排序前: ";
    for (int i = 0; i < size; i++) cout << arr[i] << " ";
    
    bubbleSort(arr, size);
    
    cout << "\n排序后: ";
    for (int i = 0; i < size; i++) cout << arr[i] << " ";
    
    return 0;
}

26. 合并有序数组

#include <iostream>
using namespace std;

void mergeArrays(int arr1[], int size1, int arr2[], int size2, int result[]) {
    int i = 0, j = 0, k = 0;
    
    while (i < size1 && j < size2) {
        if (arr1[i] < arr2[j]) {
            result[k++] = arr1[i++];
        } else {
            result[k++] = arr2[j++];
        }
    }
    
    // 处理剩余元素
    while (i < size1) result[k++] = arr1[i++];
    while (j < size2) result[k++] = arr2[j++];
}

int main() {
    int arr1[] = {1, 3, 5, 7};
    int size1 = sizeof(arr1) / sizeof(arr1[0]);
    
    int arr2[] = {2, 4, 6, 8};
    int size2 = sizeof(arr2) / sizeof(arr2[0]);
    
    int result[size1 + size2];
    mergeArrays(arr1, size1, arr2, size2, result);
    
    cout << "合并后数组: ";
    for (int i = 0; i < size1 + size2; i++) {
        cout << result[i] << " ";
    }
    return 0;
}

27. 统计元素频率

#include <iostream>
#include <unordered_map>
using namespace std;

void countFrequency(int arr[], int size) {
    unordered_map<int, int> freqMap;
    
    for (int i = 0; i < size; i++) {
        freqMap[arr[i]]++;
    }
    
    cout << "元素频率:\n";
    for (auto& pair : freqMap) {
        cout << pair.first << ": " << pair.second << "次\n";
    }
}

int main() {
    int arr[] = {1, 2, 2, 3, 3, 3, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    countFrequency(arr, size);
    return 0;
}

28. 矩阵加法

#include <iostream>
using namespace std;

const int ROWS = 3;
const int COLS = 3;

void addMatrices(int mat1[][COLS], int mat2[][COLS], int result[][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            result[i][j] = mat1[i][j] + mat2[i][j];
        }
    }
}

void printMatrix(int mat[][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            cout << mat[i][j] << "\t";
        }
        cout << endl;
    }
}

int main() {
    int mat1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int mat2[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
    int result[ROWS][COLS];
    
    addMatrices(mat1, mat2, result);
    
    cout << "矩阵1:\n"; printMatrix(mat1);
    cout << "\n矩阵2:\n"; printMatrix(mat2);
    cout << "\n相加结果:\n"; printMatrix(result);
    
    return 0;
}

29. 矩阵乘法

#include <iostream>
using namespace std;

const int ROWS1 = 2, COLS1 = 3;
const int ROWS2 = 3, COLS2 = 2;

void multiplyMatrices(int mat1[][COLS1], int mat2[][COLS2], int result[][COLS2]) {
    for (int i = 0; i < ROWS1; i++) {
        for (int j = 0; j < COLS2; j++) {
            result[i][j] = 0;
            for (int k = 0; k < COLS1; k++) {
                result[i][j] += mat1[i][k] * mat2[k][j];
            }
        }
    }
}

void printMatrix(int mat[][COLS2], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << mat[i][j] << "\t";
        }
        cout << endl;
    }
}

int main() {
    int mat1[ROWS1][COLS1] = {{1, 2, 3}, {4, 5, 6}};
    int mat2[ROWS2][COLS2] = {{7, 8}, {9, 10}, {11, 12}};
    int result[ROWS1][COLS2];
    
    multiplyMatrices(mat1, mat2, result);
    
    cout << "矩阵1:\n"; printMatrix(mat1, ROWS1, COLS1);
    cout << "\n矩阵2:\n"; printMatrix(mat2, ROWS2, COLS2);
    cout << "\n乘积结果:\n"; printMatrix(result, ROWS1, COLS2);
    
    return 0;
}

30. 字符串长度

#include <iostream>
using namespace std;

int stringLength(const char* str) {
    int length = 0;
    while (*str != '\0') {
        length++;
        str++;
    }
    return length;
}

int main() {
    char str[] = "Hello, World!";
    cout << "字符串 \"" << str << "\" 的长度是: " 
         << stringLength(str) << endl;
    
    // 使用标准库方法验证
    cout << "验证结果: " << strlen(str) << endl;
    return 0;
}

31. 字符串反转

#include <iostream>
#include <cstring> // for strlen
using namespace std;

void reverseString(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        swap(str[i], str[len - 1 - i]);
    }
}

int main() {
    char str[] = "Hello, World!";
    cout << "原字符串: " << str << endl;
    
    reverseString(str);
    cout << "反转后: " << str << endl;
    
    return 0;
}

32. 判断回文字符串

#include <iostream>
#include <cstring>
using namespace std;

bool isPalindrome(const char str[]) {
    int left = 0;
    int right = strlen(str) - 1;
    
    while (left < right) {
        if (str[left++] != str[right--]) {
            return false;
        }
    }
    return true;
}

int main() {
    char str[] = "madam";
    cout << str << (isPalindrome(str) ? "是回文" : "不是回文") << endl;
    return 0;
}

33. 统计字符类型

#include <iostream>
#include <cctype> // for isalpha, isdigit, isspace
using namespace std;

void countCharacters(const char str[]) {
    int letters = 0, digits = 0, spaces = 0, others = 0;
    
    for (int i = 0; str[i] != '\0'; i++) {
        if (isalpha(str[i])) letters++;
        else if (isdigit(str[i])) digits++;
        else if (isspace(str[i])) spaces++;
        else others++;
    }
    
    cout << "字母: " << letters << "\n数字: " << digits
         << "\n空格: " << spaces << "\n其他: " << others << endl;
}

int main() {
    char str[] = "Hello 123 World! 2024";
    countCharacters(str);
    return 0;
}

34. 字符串复制

#include <iostream>
using namespace std;

void stringCopy(char dest[], const char src[]) {
    int i = 0;
    while ((dest[i] = src[i]) != '\0') {
        i++;
    }
}

int main() {
    char src[] = "Copy this string";
    char dest[50];
    
    stringCopy(dest, src);
    cout << "复制结果: " << dest << endl;
    return 0;
}

35. 字符串连接

#include <iostream>
using namespace std;

void stringConcat(char dest[], const char src[]) {
    int destLen = 0;
    while (dest[destLen] != '\0') destLen++;
    
    int i = 0;
    while ((dest[destLen + i] = src[i]) != '\0') {
        i++;
    }
}

int main() {
    char str1[50] = "Hello, ";
    char str2[] = "World!";
    
    stringConcat(str1, str2);
    cout << "连接结果: " << str1 << endl;
    return 0;
}

36. 删除指定字符

#include <iostream>
using namespace std;

void removeChar(char str[], char ch) {
    int j = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] != ch) {
            str[j++] = str[i];
        }
    }
    str[j] = '\0';
}

int main() {
    char str[] = "Remove all 'a' characters";
    char ch = 'a';
    
    cout << "原字符串: " << str << endl;
    removeChar(str, ch);
    cout << "删除'" << ch << "'后: " << str << endl;
    return 0;
}

37. 查找子串位置

#include <iostream>
using namespace std;

int findSubstring(const char str[], const char substr[]) {
    int lenStr = 0, lenSub = 0;
    while (str[lenStr] != '\0') lenStr++;
    while (substr[lenSub] != '\0') lenSub++;
    
    for (int i = 0; i <= lenStr - lenSub; i++) {
        int j;
        for (j = 0; j < lenSub; j++) {
            if (str[i + j] != substr[j]) break;
        }
        if (j == lenSub) return i;
    }
    return -1;
}

int main() {
    char str[] = "This is a sample string";
    char substr[] = "sample";
    
    int pos = findSubstring(str, substr);
    if (pos != -1) {
        cout << "子串位于索引: " << pos << endl;
    } else {
        cout << "未找到子串" << endl;
    }
    return 0;
}

38. 字符串替换

#include <iostream>
#include <cstring>
using namespace std;

void replaceSubstring(char str[], const char oldStr[], const char newStr[]) {
    char temp[100];
    int i = 0, j = 0;
    
    while (str[i] != '\0') {
        bool match = true;
        for (int k = 0; oldStr[k] != '\0'; k++) {
            if (str[i + k] != oldStr[k]) {
                match = false;
                break;
            }
        }
        
        if (match) {
            for (int k = 0; newStr[k] != '\0'; k++) {
                temp[j++] = newStr[k];
            }
            i += strlen(oldStr);
        } else {
            temp[j++] = str[i++];
        }
    }
    temp[j] = '\0';
    strcpy(str, temp);
}

int main() {
    char str[100] = "Hello, World! World is big.";
    char oldStr[] = "World";
    char newStr[] = "Universe";
    
    cout << "原字符串: " << str << endl;
    replaceSubstring(str, oldStr, newStr);
    cout << "替换后: " << str << endl;
    return 0;
}

39. 字符串分割

#include <iostream>
#include <cstring>
using namespace std;

void splitString(const char str[], char delimiter) {
    char token[50];
    int tokenIndex = 0;
    
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == delimiter) {
            token[tokenIndex] = '\0';
            if (tokenIndex > 0) {
                cout << token << endl;
            }
            tokenIndex = 0;
        } else {
            token[tokenIndex++] = str[i];
        }
    }
    
    // 输出最后一个token
    token[tokenIndex] = '\0';
    if (tokenIndex > 0) {
        cout << token << endl;
    }
}

int main() {
    char str[] = "apple,banana,orange,grape";
    char delimiter = ',';
    
    cout << "分割结果:" << endl;
    splitString(str, delimiter);
    return 0;
}

40. 字符串大小写转换

#include <iostream>
#include <cctype>
using namespace std;

void toUpperCase(char str[]) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
}

void toLowerCase(char str[]) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = tolower(str[i]);
    }
}

int main() {
    char str[] = "Hello, World!";
    
    cout << "原字符串: " << str << endl;
    
    toUpperCase(str);
    cout << "大写: " << str << endl;
    
    toLowerCase(str);
    cout << "小写: " << str << endl;
    
    return 0;
}

41. 函数求阶乘

#include <iostream>
using namespace std;

// 函数声明
long long factorial(int n);

int main() {
    int num;
    cout << "输入非负整数: ";
    cin >> num;
    cout << num << "! = " << factorial(num) << endl;
    return 0;
}

// 函数定义
long long factorial(int n) {
    if (n < 0) return -1; // 错误处理
    long long result = 1;
    for (int i = 2; i <= n; ++i) {
        result *= i;
    }
    return result;
}

42. 递归求斐波那契数列

#include <iostream>
using namespace std;

int fibonacci(int n) {
    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int n;
    cout << "输入项数: ";
    cin >> n;
    cout << "斐波那契数列: ";
    for (int i = 0; i < n; ++i) {
        cout << fibonacci(i) << " ";
    }
    return 0;
}

43. 汉诺塔问题

#include <iostream>
using namespace std;

void hanoi(int n, char from, char to, char aux) {
    if (n == 1) {
        cout << "将盘1从 " << from << " 移到 " << to << endl;
        return;
    }
    hanoi(n - 1, from, aux, to);
    cout << "将盘" << n << "从 " << from << " 移到 " << to << endl;
    hanoi(n - 1, aux, to, from);
}

int main() {
    int disks;
    cout << "输入盘数: ";
    cin >> disks;
    hanoi(disks, 'A', 'C', 'B'); // A是源柱,C是目标柱,B是辅助柱
    return 0;
}

44. 函数重载示例

#include <iostream>
using namespace std;

// 重载1:整数相加
int add(int a, int b) {
    return a + b;
}

// 重载2:浮点数相加
double add(double a, double b) {
    return a + b;
}

// 重载3:三个整数相加
int add(int a, int b, int c) {
    return a + b + c;
}

int main() {
    cout << "整数和: " << add(5, 3) << endl;
    cout << "浮点数和: " << add(2.5, 3.7) << endl;
    cout << "三数之和: " << add(1, 2, 3) << endl;
    return 0;
}

45. 默认参数函数

#include <iostream>
using namespace std;

// 计算盒子体积(高度默认为1)
double boxVolume(double length, double width = 1, double height = 1) {
    return length * width * height;
}

int main() {
    cout << "默认参数体积: " << boxVolume(5) << endl;         // 5*1*1
    cout << "部分指定参数: " << boxVolume(5, 2) << endl;      // 5*2*1
    cout << "全指定参数: " << boxVolume(5, 2, 3) << endl;    // 5*2*3
    return 0;
}

46. 内联函数

#include <iostream>
using namespace std;

// 内联函数计算平方
inline int square(int x) {
    return x * x;
}

int main() {
    int num;
    cout << "输入整数: ";
    cin >> num;
    cout << "平方: " << square(num) << endl;
    return 0;
}

47. 函数模板

#include <iostream>
using namespace std;

// 模板函数求最大值
template <typename T>
T getMax(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    cout << "整数最大: " << getMax(3, 7) << endl;
    cout << "浮点数最大: " << getMax(3.14, 2.71) << endl;
    cout << "字符最大: " << getMax('a', 'z') << endl;
    return 0;
}

48. 函数指针示例

#include <iostream>
using namespace std;

void greetEnglish() { cout << "Hello!" << endl; }
void greetSpanish() { cout << "¡Hola!" << endl; }
void greetFrench() { cout << "Bonjour!" << endl; }

int main() {
    // 定义函数指针数组
    void (*greetings[])() = {greetEnglish, greetSpanish, greetFrench};
    
    cout << "选择语言 (1-英语 2-西班牙语 3-法语): ";
    int choice;
    cin >> choice;
    
    if (choice >= 1 && choice <= 3) {
        greetings; // 通过指针调用函数
    } else {
        cout << "无效选择" << endl;
    }
    return 0;
}

49. Lambda表达式排序

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    vector<int> numbers = {4, 2, 5, 1, 3};
    
    // 使用lambda表达式自定义排序(降序)
    sort(numbers.begin(), numbers.end(), int a, int b {
        return a > b;
    });
    
    cout << "降序排序结果: ";
    for (int num : numbers) {
        cout << num << " ";
    }
    return 0;
}

50. 递归求最大公约数

#include <iostream>
using namespace std;

// 递归实现辗转相除法
int gcd(int a, int b) {
    return (b == 0) ? a : gcd(b, a % b);
}

int main() {
    int a, b;
    cout << "输入两个正整数: ";
    cin >> a >> b;
    cout << "GCD: " << gcd(a, b) << endl;
    return 0;
}
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐