Tsuki泱 2023-09-30 16:32 采纳率: 0%
浏览 14
问题最晚将于10月08日00:00点结题

如何用c++编写数学规律题

1 求12345
2 有50个学生,要求输出成绩在80分以上的学生的学号和成绩
3 判定2000-2500年中的每一年是否为闰年,并将结果输出
4 求1-1/2+1/3-1/4+…+1/99-1/100

  • 写回答

8条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-09-30 16:38
    关注

    以下回复引自强大而免费的微信小程序:皆我百晓生。如有帮助,还望采纳。


    以下是使用 C++ 编写这些数学规律题的代码示例:

    1. 求12345:
    #include <iostream>
    using namespace std;
    
    int main() {
        int sum = 0;
    
        for (int i = 1; i <= 12345; i++) {
            sum += i;
        }
    
        cout << "求和结果: " << sum << endl;
    
        return 0;
    }
    
    1. 有50个学生,要求输出成绩在80分以上的学生的学号和成绩:
    #include <iostream>
    using namespace std;
    
    struct Student {
        int id;
        int score;
    };
    
    int main() {
        const int NUM_STUDENTS = 50;
        Student students[NUM_STUDENTS];
    
        for (int i = 0; i < NUM_STUDENTS; i++) {
            cout << "请输入第 " << i + 1 << " 个学生的学号和成绩: ";
            cin >> students[i].id >> students[i].score;
        }
        
        cout << "成绩在80分以上的学生的学号和成绩:" << endl;
        for (int i = 0; i < NUM_STUDENTS; i++) {
            if (students[i].score >= 80) {
                cout << "学生 " << students[i].id << " 的成绩为 " << students[i].score << endl;
            }
        }
    
        return 0;
    }
    
    1. 判定2000-2500年中的每一年是否为闰年,并将结果输出:
    #include <iostream>
    using namespace std;
    
    bool isLeapYear(int year) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    
    int main() {
        for (int year = 2000; year <= 2500; year++) {
            if (isLeapYear(year)) {
                cout << year << "年是闰年" << endl;
            } else {
                cout << year << "年不是闰年" << endl;
            }
        }
    
        return 0;
    }
    
    1. 求1-1/2+1/3-1/4+…+1/99-1/100:
    #include <iostream>
    using namespace std;
    
    int main() {
        double result = 0;
    
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                result -= 1.0 / i;
            } else {
                result += 1.0 / i;
            }
        }
    
        cout << "求和结果: " << result << endl;
    
        return 0;
    }
    

    希望这些示例能帮助到你!

    评论 编辑记录

报告相同问题?

问题事件

  • 已采纳回答 9月30日
  • 创建了问题 9月30日