有 n 个小朋友去果园郊游,现在他们在玩一个比重量的游戏。他们规定在果园找苹果和西瓜这两种水果,然后统计出各自摘的苹果和西瓜的总重量,再根据n个总重量来排名,总重量高的排在前面(如果出现总重量相同就按苹果重的往前排)。现在请你输出排序后的结果。
样例输入:
4
2 2
1 4
3 1
2 1
样例输入:
1 4
3 1
2 2
2 1

XJOI 9165果园大比拼题解
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注
让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
首先,根据题目要求,我们需要定义一个结构体来存储小朋友的信息,包括摘的苹果数量、西瓜数量、总重量和排名。然后根据总重量和苹果数量进行排序。在比较总重量的时候,如果总重量相同就比较苹果的重量。 下面是一个C++的示例代码:#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Child { int apples; int watermelons; int totalWeight; int rank; }; bool compare(const Child &a, const Child &b) { if (a.totalWeight == b.totalWeight) { return a.apples > b.apples; } else { return a.totalWeight > b.totalWeight; } } int main() { int n; cin >> n; vector<Child> children(n); for (int i = 0; i < n; i++) { cin >> children[i].apples >> children[i].watermelons; children[i].totalWeight = children[i].apples + children[i].watermelons; } sort(children.begin(), children.end(), compare); for (int i = 0; i < n; i++) { cout << children[i].apples << " " << children[i].watermelons << endl; } return 0; }
输入:
4 2 2 1 4 3 1 2 1
输出:
1 4 3 1 2 2 2 1
解决 无用评论 打赏 举报