题意:是把颜色由多到少进行排序,从大到小的输出。
案例:
Sample Input
3
3
red 1
green 2
yellow 3
1
blue 83
2
red 2
white 1
Sample Output
yellow green red
blue
red white
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
char ch[110];
int num;}s[110];
bool cmp(node a,node b)
{
return a.num>b.num;
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int n;
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%s%d",s[i].ch,&s[i].num);
}
sort(s,s+n,cmp);
for (int i=0;i<n;i++)
{
printf("%s%c",s[i].ch,i!=n-1?' ':'\n');
}
}
return 0;
}
这里利用结构体通过对颜色的数量排序,在输出最多个数对应的颜色
另外在这里学到了一种控制空格与输出的方式。