1 求12345
2 有50个学生,要求输出成绩在80分以上的学生的学号和成绩
3 判定2000-2500年中的每一年是否为闰年,并将结果输出
4 求1-1/2+1/3-1/4+…+1/99-1/100
如何用c++编写数学规律题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
8条回答 默认 最新
关注以下回复引自强大而免费的微信小程序:皆我百晓生。如有帮助,还望采纳。
以下是使用 C++ 编写这些数学规律题的代码示例:
- 求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; }- 有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; }- 判定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/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; }希望这些示例能帮助到你!
评论 打赏 举报 编辑记录解决 1无用