20个新手学习c++必会的程序 输出*三角形、杨辉三角等(附代码)

示例 1: Hello World

#include 
using namespace std;

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

示例 2: 计算两个数的和

#include 
using namespace std;

int main() {
    int a = 5;
    int b = 10;
    int sum = a + b;
    cout << "Sum is: " << sum << endl;
    return 0;
}

示例 3: 使用函数计算阶乘

#include 
using namespace std;

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int num = 5;
    cout << "Factorial of " << num << " is " << factorial(num) << endl;
    return 0;
}

示例 4: 打印Fibonacci序列

#include 
using namespace std;

void printFibonacci(int n) {
    int t1 = 0, t2 = 1;
    cout << t1 << " " << t2 << " ";

    for (int i = 2; i <= n; ++i) {
        int next = t1 + t2;
        cout << next << " ";
        t1 = t2;
        t2 = next;
    }
}

int main() {
    int n = 10;
    printFibonacci(n);
    return 0;
}

示例 5: 检查一个数是否为素数

#include 
using namespace std;

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

int main() {
    int num = 11;
    if (isPrime(num)) {
        cout << num << " is a prime number." << endl;
    } else {
        cout << num << " is not a prime number." << endl;
    }
    return 0;
}

示例 6: 计算圆的面积

#include 
#include 
using namespace std;

double circleArea(double r) {
    return M_PI * pow(r, 2);
}

int main() {
    double radius = 5.0;
    cout << "Area of the circle is: " << circleArea(radius) << endl;
    return 0;
}

示例 7: 排序数组

#include 
#include 
using namespace std;

int main() {
    int arr[] = {5, 3, 8, 4, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    sort(arr, arr+n);
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
    return 0;
}

示例 8: 字符串反转

#include 
#include 
using namespace std;

int main() {
    string str = "hello";
    reverse(str.begin(), str.end());
    cout << "Reversed string is: " << str << endl;
    return 0;
}

示例 9: 数组查找

#include 
#include 
using namespace std;

int findNumber(vector& vec, int target) {
    for (int i = 0; i < vec.size(); ++i) {
        if (vec[i] == target) return i;
    }
    return -1;
}

int main() {
    vector vec = {1, 2, 3, 4, 5};
    int target = 3;
    int index = findNumber(vec, target);
    if (index != -1) {
        cout << "Element found at index: " << index << endl;
    } else {
        cout << "Element not found." << endl;
    }
    return 0;
}

示例 10: 从字符串中删除特定字符

#include 
#include 
using namespace std;

string removeChar(string s, char c) {
    string result = "";
    for (char ch : s) {
        if (ch != c) result += ch;
    }
    return result;
}

int main() {
    string str = "example string";
    char ch = 'e';
    cout << "Original string: " << str << endl;
    cout << "String after removing '" << ch << "': " << removeChar(str, ch) << endl;
    return 0;
}

示例 11: 输出正三角形

#include 
using namespace std;

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

int main() {
    int n = 5;
    printTriangle(n);
    return 0;
}

示例 12: 输出倒三角形

#include 
using namespace std;

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

int main() {
    int n = 5;
    printReverseTriangle(n);
    return 0;
}

示例 13: 斐波那契数列(迭代版)

#include 
using namespace std;

void fibonacci(int n) {
    int t1 = 0, t2 = 1;
    cout << t1 << " " << t2 << " ";

    for (int i = 2; i < n; ++i) {
        int next = t1 + t2;
        cout << next << " ";
        t1 = t2;
        t2 = next;
    }
}

int main() {
    int n = 10;
    fibonacci(n);
    return 0;
}

示例 14: 求解一元二次方程

#include 
#include 
using namespace std;

void solveQuadraticEquation(double a, double b, double c) {
    double discriminant = b * b - 4 * a * c;
    if (discriminant < 0) {
        cout << "No real roots" << endl;
    } else {
        double root1 = (-b + sqrt(discriminant)) / (2 * a);
        double root2 = (-b - sqrt(discriminant)) / (2 * a);
        cout << "Roots are: " << root1 << " and " << root2 << endl;
    }
}

int main() {
    solveQuadraticEquation(1, -3, 2);
    return 0;
}

示例 15: 判断一个数是否为回文数

#include 
using namespace std;

bool isPalindrome(int x) {
    int original = x, reversed = 0;
    while (x > 0) {
        reversed = reversed * 10 + x % 10;
        x /= 10;
    }
    return original == reversed;
}

int main() {
    int num = 121;
    if (isPalindrome(num)) {
        cout << num << " is a palindrome." << endl;
    } else {
        cout << num << " is not a palindrome." << endl;
    }
    return 0;
}

示例 16: 输出杨辉三角

#include 
using namespace std;

void printPascalTriangle(int n) {
    for (int line = 1; line <= n; ++line) {
        int C = 1;
        for (int i = 1; i <= line; ++i) {
            cout << C << " ";
            C = C * (line - i) / i;
        }
        cout << endl;
    }
}

int main() {
    int n = 5;
    printPascalTriangle(n);
    return 0;
}

示例 17: 模拟投掷骰子

#include 
#include 
#include 
using namespace std;

int rollDice() {
    srand(time(nullptr));
    return rand() % 6 + 1;
}

int main() {
    int dice = rollDice();
    cout << "Rolled dice: " << dice << endl;
    return 0;
}

示例 18: 计算平均值

#include 
using namespace std;

double calculateAverage(int arr[], int size) {
    int sum = 0;
    for (int i = 0; i < size; ++i) {
        sum += arr[i];
    }
    return static_cast(sum) / size;
}

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int size = sizeof(arr) / sizeof(arr[0]);
    double avg = calculateAverage(arr, size);
    cout << "Average: " << avg << endl;
    return 0;
}

示例 19: 生成随机数序列

#include 
#include 
#include 
using namespace std;

void generateRandomNumbers(int arr[], int size) {
    srand(time(nullptr));
    for (int i = 0; i < size; ++i) {
        arr[i] = rand() % 100;
    }
}

int main() {
    int arr[10];
    generateRandomNumbers(arr, 10);
    for (int i = 0; i < 10; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
    return 0;
}

 示例 20: 字符串拼接

#include 
#include 
using namespace std;

string concatenateStrings(const string& s1, const string& s2) {
    return s1 + " " + s2;
}

int main() {
    string s1 = "Hello";
    string s2 = "World";
    cout << "Concatenated string: " << concatenateStrings(s1, s2) << endl;
    return 0;
}

你可能感兴趣的:(c++,学习,算法,大学生,开发语言,数据结构)