C++经典程序

在C++编程中,有几个被广泛认为是“经典”的程序。这些程序经常被用来教授C++的基础概念、演示特定的编程技巧,或者作为初学者学习和实践的好例子。下面是一些C++中的经典程序:

  1. Hello World程序

    #include <iostream>
    using namespace std;
    
    int main() {
         
        cout << "Hello, World!" << endl;
        return 0;
    }
    

    这是最基本的C++程序,用于演示如何输出文本到控制台。

  2. 阶乘程序

    #include <iostream>
    using namespace std;
    
    int factorial(int n) {
         
        if (n == 0) return 1;
        return n * factorial(n - 1);
    }
    
    int main() {
         
        int n;
        cout << "Enter a positive integer: ";
        cin >> n;
        cout << "Factorial of " << n << " = " << factorial(n) << endl;
        return 0;
    }
    

    这个程序展示了递归函数的使用,计算给定数的阶乘。

  3. Fibonacci数列程序

    #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 << "Enter the number of terms: ";
        cin >> n;
        cout << "Fibonacci Series: ";
        for (int i = 0; i < n; i++) {
         
            cout << fibonacci(i) << " ";
        }
        cout << endl;
        return 0;
    }
    

    这个程序通过递归函数生成Fibonacci数列。

  4. 排序算法(如冒泡排序)

    #include <iostream>
    using namespace std;
    
    void bubbleSort(int arr[], int n) {
         
        for (int i = 0; i < n-1; i++)     
            for (int j = 0; j < n-i-1; 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 n = sizeof(arr)/sizeof(arr[0]);
        bubbleSort(arr, n);
        cout << "Sorted array: \n";
        for (int i=0; i < n; i++)
            cout << arr[i] << " ";
        cout << endl;
        return 0;
    }
    

    这个程序演示了基本的冒泡排序算法。

这些程序展示了C++的基本结构和一些常见的编程概念,如循环、递归、数组处理等。对于初学者来说,理解和实践这些程序是学习C++的一个很好的起点。

相关推荐

  1. C++经典程序(2)

    2024-01-11 12:48:07       33 阅读
  2. C++经典程序

    2024-01-11 12:48:07       27 阅读
  3. C++经典程序

    2024-01-11 12:48:07       35 阅读
  4. c经典程序

    2024-01-11 12:48:07       35 阅读
  5. 探寻C++经典程序之美

    2024-01-11 12:48:07       29 阅读
  6. C#程序反编译经验总结

    2024-01-11 12:48:07       31 阅读
  7. 经典C语言程序之 编程】详细解析及示例代码

    2024-01-11 12:48:07       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-11 12:48:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-11 12:48:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-11 12:48:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-11 12:48:07       18 阅读

热门阅读

  1. 【金融支付】常用术语和定义

    2024-01-11 12:48:07       32 阅读
  2. 一、SpringBoot框架搭建

    2024-01-11 12:48:07       34 阅读
  3. C/C++-传值/地址的区别

    2024-01-11 12:48:07       28 阅读
  4. 在IntelliJ IDEA中,.idea文件是什么,可以删除吗

    2024-01-11 12:48:07       33 阅读
  5. Crow:路由局部插件2 调用before_handle

    2024-01-11 12:48:07       40 阅读
  6. C++入门级程序day1

    2024-01-11 12:48:07       36 阅读
  7. Python Selenium常见的报错以及措施

    2024-01-11 12:48:07       33 阅读