这道洛谷P2433题目集合了14个不同类型的小学数学问题,涵盖了基础运算、几何计算、追及问题、逻辑推理等多个数学知识点。作为编程练习题,它要求我们不仅要理解数学原理,还要能够用C++代码实现这些计算过程。
这类题目对编程初学者特别有价值,因为它:
问题1是最简单的字符串输出,考察基本的cout用法:
cpp复制cout << "I love Luogu!";
问题2考察基础算术运算:
cpp复制int total = 10;
int a = 2, uim = 4;
cout << a + uim << " " << total - a - uim;
问题3涉及整数除法和取余运算:
cpp复制int apples = 14, students = 4;
int per_student = apples / students; // 整数除法
int distributed = per_student * students;
int remaining = apples - distributed;
问题4需要注意浮点数精度控制:
cpp复制cout << fixed << setprecision(6) << 500.0 / 3;
关键点:必须使用fixed和setprecision(6)确保输出6位小数,不使用科学计数法
问题5是火车相遇问题:
cpp复制int total_length = 260 + 220;
int relative_speed = 12 + 20;
cout << total_length / relative_speed; // 结果正好是整数
问题6计算长方形对角线(勾股定理):
cpp复制cout << sqrt(6*6 + 9*9);
问题8涉及圆的几何计算:
cpp复制double r = 5;
cout << 2 * PI * r << endl; // 周长
cout << PI * r * r << endl; // 面积
cout << 4.0/3 * PI * r * r * r; // 体积
问题13是体积转换问题:
cpp复制double v1 = 4.0/3 * PI * 4*4*4;
double v2 = 4.0/3 * PI * 10*10*10;
double edge = pow(v1 + v2, 1.0/3);
cout << trunc(edge); // 舍去小数部分
问题7是简单的账户余额计算:
cpp复制int balance = 100;
cout << balance + 10 << endl; // 存款
cout << balance + 10 - 20 << endl; // 消费
cout << 0 << endl; // 全部取出
问题9需要逆向推导桃子数量:
cpp复制int day4 = 1;
int day3 = (day4 + 1) * 2;
int day2 = (day3 + 1) * 2;
int day1 = (day2 + 1) * 2;
cout << day1;
问题10是工作率问题,可以建立方程求解:
cpp复制// 设初始队列有x个任务,每分钟新增y个
// 8*30 = x + 30y
// 10*6 = x + 6y
// 解得:y = 5/3, x = 50
// 10分钟需要机器数:(x + 10y)/10 = 28/3 ≈ 9.333
// 题目要求刚好完成,所以需要向上取整为10台
cout << 10;
问题11是简单的追及问题:
cpp复制double speed_diff = 8 - 5;
cout << 100 / speed_diff;
问题14是二次方程求最优解:
cpp复制// 设降价x元,则价格=110-x,人数=10+x
// 收入=(110-x)(10+x)=3500
// 解方程得x^2-100x+2400=0
// x=40或60,取较小的x=40
// 所以定价=110-40=70
cout << 70;
题目要求根据输入的问题编号输出对应答案,典型的switch-case结构:
cpp复制int T;
cin >> T;
switch(T) {
case 1: /* 问题1代码 */ break;
case 2: /* 问题2代码 */ break;
// ...其他case
default: break;
}
cpp复制#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#define PI 3.141593
using namespace std;
int main() {
int T;
cin >> T;
if (T == 1) {
cout << "I love Luogu!";
} else if (T == 2) {
cout << 2 + 4 << " " << 10 - 2 - 4;
} else if (T == 3) {
int num = 14, stu = 4;
int per = num / stu;
cout << per << endl << per * stu << endl << num - per * stu;
} else if (T == 4) {
cout << fixed << setprecision(6) << 500.0 / 3;
} else if (T == 5) {
cout << (260 + 220) / (12 + 20);
} else if (T == 6) {
cout << sqrt(6*6 + 9*9);
} else if (T == 7) {
int balance = 100;
cout << balance + 10 << endl;
cout << balance + 10 - 20 << endl;
cout << 0;
} else if (T == 8) {
double r = 5;
cout << 2 * PI * r << endl;
cout << PI * r * r << endl;
cout << 4.0/3 * PI * r * r * r;
} else if (T == 9) {
int day4 = 1;
int day3 = (day4 + 1) * 2;
int day2 = (day3 + 1) * 2;
int day1 = (day2 + 1) * 2;
cout << day1;
} else if (T == 10) {
cout << 10;
} else if (T == 11) {
cout << 100.0 / (8 - 5);
} else if (T == 12) {
cout << 'M' - 'A' + 1 << endl;
cout << (char)('A' + 17);
} else if (T == 13) {
double v = 4.0/3*PI*(4*4*4 + 10*10*10);
cout << (int)pow(v, 1.0/3);
} else if (T == 14) {
cout << 70;
}
return 0;
}
这类题目虽然基础,但涵盖了编程竞赛中的许多核心技能。通过系统练习,可以快速提升将数学问题转化为代码的能力,为更复杂的算法问题打下坚实基础。