为了可持续发展,让别人也可用此题作为训练题,便不搬题目上来了。
原题链接:https://pintia.cn/problem-sets/1375335383906332672/problems/1375380533974085632
思路:
利用Map容器存储小猫名字与最大最小推荐体重的关系(用了一个结构体包装,易于管理),所以本题考查的是字符串映射和数据归纳的能力,C++有很多STL可以加快我们的代码编写速度,例如map、vector、queue、stack等。
见码知意,便不再赘述。
参考代码:
#include<bits/stdc++.h>
using namespace std;
struct CatInfo {
double min, max, good;
double getFoodCalories() {
return 13.6 * good + 70;
}
string getCase(double weight) {
if(weight < min) return "underweight";
else if(weight > max) return "overweight";
else return "OK";
}
};
int main() {
int n,k;
cin>>n>>k;
map<string, CatInfo> catInfos;
while(k--) {
string name;
cin>>name;
cin>>catInfos[name].min>>catInfos[name].max>>catInfos[name].good;
}
while(n--) {
string name;
double weight;
cin>>name>>weight;
cout<<catInfos[name].getCase(weight)<<" ";
printf("%.1f\n", catInfos[name].getFoodCalories());
}
return 0;
}