📜  C++程序的输出| 17套(1)

📅  最后修改于: 2023-12-03 15:29:55.540000             🧑  作者: Mango

C++程序的输出 | 17套

简介

本文为程序员提供17个C++程序的输出练习,旨在提高程序员的编程能力和调试技巧。每个程序都包含一个特定的输出,程序员需要阅读代码,理解程序逻辑,以及调试并运行程序。练习的程序都包含了一些常用的C++语法,涉及到字符串、循环、条件判断、数组等基础知识。

程序列表
  1. 输出"Hello, World!"
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}
  1. 输出两个数的和
#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 20;
    int sum = a + b;

    cout << "The sum of " << a << " and " << b << " is " << sum << endl;
    return 0;
}
  1. 输出10个奇数
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
        cout << 2 * i - 1 << " ";
    }

    cout << endl;
    return 0;
}
  1. 输出乘法表
#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 << " ";
        }
        cout << endl;
    }

    return 0;
}
  1. 输出100以内的素数
#include <iostream>
#include <cmath>
using namespace std;

bool is_prime(int n) {
    if (n <= 1) {
        return false;
    }

    int sq = sqrt(n);

    for (int i = 2; i <= sq; i++) {
        if (n % i == 0) {
            return false;
        }
    }

    return true;
}

int main() {
    for (int i = 2; i < 100; i++) {
        if (is_prime(i)) {
            cout << i << " ";
        }
    }

    cout << endl;
    return 0;
}
  1. 输出Fibonacci数列
#include <iostream>
using namespace std;

int main() {
    int a = 0;
    int b = 1;
    int n;

    cout << "How many Fibonacci numbers do you want to generate?" << endl;
    cin >> n;

    for (int i = 0; i < n; i++) {
        cout << b << " ";
        int temp = b;
        b = a + b;
        a = temp;
    }

    cout << endl;
    return 0;
}
  1. 输出字符串
#include <iostream>
using namespace std;

int main() {
    char name[] = "John";
    cout << "My name is " << name << endl;
    return 0;
}
  1. 输出正弦曲线
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    for (double x = 0; x <= 2 * M_PI; x += 0.1) {
        cout << sin(x) << endl;
    }

    return 0;
}
  1. 输出倒三角形
#include <iostream>
using namespace std;

int main() {
    int n = 5;
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}
  1. 输出正三角形

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                cout << " ";
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                cout << "*";
            }
            cout << endl;
        }
    
        return 0;
    }
    
  2. 输出阶乘

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = 5;
        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
    
        cout << n << "! = " << factorial << endl;
        return 0;
    }
    
  3. 输出最大值

    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 10;
        int b = 20;
        int max = (a > b) ? a : b;
    
        cout << "The maximum of " << a << " and " << b << " is " << max << endl;
        return 0;
    }
    
  4. 输出数组元素

    #include <iostream>
    using namespace std;
    
    int main() {
        int array[] = {1, 2, 3, 4, 5};
        int size = sizeof(array) / sizeof(array[0]);
    
        for (int i = 0; i < size; i++) {
            cout << array[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    
  5. 输出矩阵相乘结果

    #include <iostream>
    using namespace std;
    
    int main() {
        int a[][2] = {{1, 2}, {3, 4}};
        int b[][2] = {{5, 6}, {7, 8}};
        int c[2][2] = {0};
    
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    c[i][j] += a[i][k] * b[k][j];
                }
            }
        }
    
        cout << "Matrix A:" << endl;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    
        cout << "Matrix B:" << endl;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                cout << b[i][j] << " ";
            }
            cout << endl;
        }
    
        cout << "Matrix A * B:" << endl;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                cout << c[i][j] << " ";
            }
            cout << endl;
        }
    
        return 0;
    }
    
  6. 输出字符串长度

    #include <iostream>
    using namespace std;
    
    int main() {
        char str[] = "Hello, World!";
        int size = sizeof(str) / sizeof(str[0]) - 1;
    
        cout << "The length of \"" << str << "\" is " << size << endl;
        return 0;
    }
    
  7. 输出文件内容

    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        ifstream infile;
        infile.open("example.txt");
    
        if (!infile) {
            cout << "Unable to open file" << endl;
            return 1;
        }
    
        char c;
        while (infile.get(c)) {
            cout << c;
        }
    
        infile.close();
        return 0;
    }
    
  8. 输出随机数

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main() {
        srand(time(0));
    
        for (int i = 0; i < 10; i++) {
            cout << rand() << " ";
        }
    
        cout << endl;
        return 0;
    }
    
结束语

以上17个C++程序的输出练习涵盖了一些常用的语法和算法,程序员通过练习可以加深对C++语言的理解和掌握,提高编程能力和调试技巧。希望本文对读者有所帮助。