1 条题解
-
0
题目分析:
这道题目要求我们根据美式橄榄球运动员的身体属性(速度、体重、力量)来判断他们适合的场上位置。题目给出了三个具体位置及其对应的属性要求:
外接手(Wide Receiver):需要速度不超过,体重不低于,力量不低于
线锋(Lineman):需要速度不超过,体重不低于,力量不低于
四分卫(Quarterback):需要速度不超过,体重不低于,力量不低于
解题思路:
数据结构设计:首先需要定义位置的结构,包含位置名称和三个属性要求
匹配逻辑:对于每个球员,检查其属性是否满足每个位置的要求
输出处理:按要求格式输出结果,注意多个位置的顺序和空情况的处理
解题方法:
采用直接的枚举法来解决这个问题:
预处理:将三个位置及其要求存储在数组中
逐个检查:对每个球员,依次检查三个位置的属性要求
结果收集:收集所有满足条件的位置名称 输出格式化:按要求格式输出结果
C++代码实现如下:
#include <iostream> #include <vector> #include <string> using namespace std; struct Position { string name; double maxSpeed; double minWeight; double minStrength; }; // 初始化positions数组 Position positionArray[] = { {"Wide Receiver", 4.5, 150, 200}, {"Lineman", 6.0, 300, 500}, {"Quarterback", 5.0, 200, 300} }; const int POSITION_COUNT = sizeof(positionArray)/sizeof(positionArray[0]); vector<string> getPositions(double speed, double weight, double strength) { vector<string> result; for (int i = 0; i < POSITION_COUNT; ++i) { const Position& pos = positionArray[i]; if (speed <= pos.maxSpeed && weight >= pos.minWeight && strength >= pos.minStrength) { result.push_back(pos.name); } } return result; } int main() { double s, w, st; while (cin >> s >> w >> st && (s != 0 || w != 0 || st != 0)) { vector<string> pos = getPositions(s, w, st); if (pos.empty()) { cout << "No positions" << endl; } else { for (vector<string>::iterator it = pos.begin(); it != pos.end(); ++it) { cout << *it << " "; } cout << endl; } } return 0; }
- 1
信息
- ID
- 1071
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 3
- 已通过
- 1
- 上传者