题目描述
从现在开始,拟定属于你自己的健康饮食方案吧! 一份全新的健康饮食计划,将会使
你浑身充满活力。如果长时间坚持这样的饮食,那么不仅你患疾病的几率降低,而且还会比
预期寿命延长15 年。当然,随之而来的,还有你一直向往的好身材。假设供选择的菜单如
下:每份热量单位:大卡/100 克
假设供选择的菜单如下:每份热量单位:大卡/100 克
Boiled Fish with Pickled Cabbage and Chili 92
Sauted Sliced Pork with Pepper and Chili , Sichuan Style 231
Cold Noodles Sichuan Style 260
Fish Filets in Hot Chili Oil 239
Steamed Rice Rolls 196
Kung Pao Chicken 313
Hot and Sour Rice Noodles 144
Soybean Milk 92
Steamed Rice 31
Scrambled Egg with Scallion 81
Steamed Jiaozi 119
Sauted Sliced Pork with Black Fungus 143
Scrambled Egg with Leek 128
Steamed Bun Stuffed with Red Bean Paste 128
Yu-Shiang Shredded Pork (Sauted with Spicy Garlic Sauce) 134
比如我们定义一个结构体含有学生的姓名和成绩的结构体Student,然后我们按照每个学生的成绩从高到底进行排序。首先我们将结构体定义为:
struct Student{
string name;
int score;
Student() {}
Student(string n,int s):name(n),score(s) {}
};
根据排序要求我们可以将排序准则函数写为:
bool cmp_score(Student x,Student y){
return x.score > y.score;
}
string 可以用等号复制字符串,但必须用cout输出
#include <bits/stdc++.h>
using namespace std;
struct menu
{
string name;
int ka;
} menus[15];
bool cmp(menu x, menu y)
{
return x.ka < y.ka;
}
int main()
{
menus[0].name = "Boiled Fish with Pickled Cabbage and Chili ";
menus[0].ka = 92;
menus[1].name = "Sauted Sliced Pork with Pepper and Chili , Sichuan Style ";
menus[1].ka = 231;
menus[2].name = "Cold Noodles Sichuan Style ";
menus[2].ka = 260;
menus[3].name = "Fish Filets in Hot Chili Oil ";
menus[3].ka = 239;
menus[4].name = "Steamed Rice Rolls ";
menus[4].ka = 196;
menus[5].name = "Kung Pao Chicken ";
menus[5].ka = 313;
menus[6].name = "Hot and Sour Rice Noodles ";
menus[6].ka = 144;
menus[7].name = "Soybean Milk ";
menus[7].ka = 92;
menus[8].name = "Steamed Rice ";
menus[8].ka = 31;
menus[9].name = "Scrambled Egg with Scallion ";
menus[9].ka = 81;
menus[10].name = "Steamed Jiaozi ";
menus[10].ka = 119;
menus[11].name = "Sauted Sliced Pork with Black Fungus ";
menus[11].ka = 143;
menus[12].name = "Scrambled Egg with Leek ";
menus[12].ka = 128;
menus[13].name = "Steamed Bun Stuffed with Red Bean Paste ";
menus[13].ka = 128;
menus[14].name = "Yu-Shiang Shredded Pork (Sauted with Spicy Garlic Sauce) ";
menus[14].ka = 134;
sort(menus, menus + 15, cmp);//从小到大排序
int n;
cin>>n;
int sum=0;
for (int i = 0; i < 15; i++)
{
sum+=menus[i].ka;
if(sum<=n)
cout << menus[i].name << menus[i].ka << endl;
else
break;
}
return 0;
}