如何用ppt形式做网站,怎么自己注册网站平台了,前几年做啥网站致富,wordpress4.x版本【题目来源】https://www.acwing.com/problem/content/3428/【题目描述】 N 只小白鼠#xff0c;每只鼠头上戴着一顶有颜色的帽子。 现在称出每只白鼠的重量#xff0c;要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色。 帽子的颜色用 red#xff0c;blue 等字符串来…【题目来源】https://www.acwing.com/problem/content/3428/【题目描述】 N 只小白鼠每只鼠头上戴着一顶有颜色的帽子。 现在称出每只白鼠的重量要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色。 帽子的颜色用 redblue 等字符串来表示。 不同的小白鼠可以戴相同颜色的帽子。 白鼠的重量用整数表示。【输入格式】 第一行为一个整数 N表示小白鼠的数目。 下面有 N 行每行是一只白鼠的信息。第一个为不大于 100 的正整数表示白鼠的重量第二个为字符串表示白鼠的帽子颜色字符串长度不超过 10 个字符。 注意白鼠的重量各不相同。【输出格式】 按照白鼠的重量从大到小的顺序输出白鼠的帽子颜色。【数据范围】 1≤N≤100【输入样例】 3 30 red 50 blue 40 green【输出样例】 blue green red【算法分析】 一、本题的一种实现方法需要按结构体某一字段对结构体数组进行排序详见https://blog.csdn.net/hnjzsyjyj/article/details/120184972例如若自定义的结构体 Person 的内容如下
struct Person {string name;int age;float height;
};
则可自定义的递增函数 up()、递减函数 down() 的内容如下
int up(Person u,Person v) { //ascending by heightif(u.heightv.height) return u.namev.name; //If equal,ascending by namereturn u.heightv.height;
}int down(Person u,Person v) { //descending by heightif(u.heightv.height) return u.namev.name; //If equal,descending by namereturn u.heightv.height;
}
若 p 为结构体数组则在主函数中调用递增函数 up()、递减函数 down() 的代码如下
sort(p,pn,up); //Sort the structured array p by ascending field heightsort(p,pn,down); //Sort the structured array p by descending field height
二、本题的另一种实现方法是利用STL pair然后调用 sort() 函数对 STL pair 进行排序。需要注意的是sort() 函数默认是按照 pair 的 first 域进行升序排序。如果 first 域相同则按照 second 域进行升序排序。 若 p 为 pair 数组则对其 first 域默认进行升序排序的代码如下
sort(p,pn); //By default, ascending by the first field of array p【算法代码一结构体排序】
#include bits/stdc.h
using namespace std;
const int maxn105;struct node {int w;string color;
} a[maxn];bool down(node a,node b){return a.wb.w; //descending by weight
}int main(){int n;cinn;for(int i1;in;i){cina[i].wa[i].color;}sort(a1,a1n,down);for(int i1;in;i){couta[i].colorendl;}
}/*
in:
3
30 red
50 blue
40 greenout:
blue
green
red
*/【算法代码二STL pair】
#include bits/stdc.h
using namespace std;const int maxn105;
pairint,string q[maxn];int main() {int n;cinn;for(int i0; in; i) {cinq[i].firstq[i].second;}sort(q,qn); //By default, ascending by the first fieldfor(int in-1; i0; i--) {coutq[i].secondendl;}return 0;
}/*
in:
3
30 red
50 blue
40 greenout:
blue
green
red
*/
【参考文献】https://blog.csdn.net/hnjzsyjyj/article/details/120184972https://blog.csdn.net/qq_27538633/article/details/126441458