首页 > 试题广场 >

小白鼠排队

[编程题]小白鼠排队
  • 热度指数:21004 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子。现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色。帽子的颜色用“red”,“blue”等字符串来表示。不同的小白鼠可以戴相同颜色的帽子。白鼠的重量用整数表示。

输入描述:
多案例输入,每个案例的输入第一行为一个整数N,表示小白鼠的数目。
下面有N行,每行是一只白鼠的信息。第一个为不大于100的正整数,表示白鼠的重量,;第二个为字符串,表示白鼠的帽子颜色,字符串长度不超过10个字符。

注意:白鼠的重量各不相同。


输出描述:
每个案例按照白鼠的重量从大到小的顺序输出白鼠的帽子颜色。
示例1

输入

3
30 red
50 blue
40 green

输出

blue
green
red
#include<bits/stdc++.h> //万能头
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
struct laoshu
{
	int weight;
	string color;
}; 
int cmp(laoshu re1, laoshu re2)
{
    return re1.weight > re2.weight;
} 
int main(){
	int n;
	while((scanf("%d",&n))!=EOF)
	{
		laoshu a[n];
		for(int i=0;i<n;i++)
		{
			cin >> a[i].weight>>a[i].color; 
		}
		sort(a,a+n,cmp);
		for(int i=0;i<n;i++)
		{
			cout << a[i].color <<endl;
		}
	}
    return 0;
}

发表于 2020-09-10 09:48:59 回复(0)
桶排序。
#include <bits/stdc++.h>
using namespace std;
int main(){
    for(int N,w;cin>>N && N;){
        vector<vector<string> > vs(101,vector<string>(0,""));
        for(string s;N-- && cin>>w>>s;vs[w].push_back(s));
        for(w=100;w;--w)
        	for(int i=0;i<vs[w].size();cout<<vs[w][i++]<<endl);
    }
    return 0;
} 


发表于 2016-09-05 13:03:18 回复(0)
题目中明确说明小白鼠的重量不大于100,数量又不大,这是典型的桶排序思想,
import java.util.Scanner;
/*题目中明确说明小白鼠的重量不大于100,数量又不大,这是典型的桶排序思想*/
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int cnt = in.nextInt();
MouseBucket[] buckets = new MouseBucket[101];
for (int i = 0; i < cnt; i++) {
MouseBucket mouse = new MouseBucket();
mouse.weight = in.nextInt();
mouse.color = in.next();
buckets[mouse.weight] = mouse;
}
for (int i =100; i >=0; i--) {
if (buckets[i]!=null) {
System.out.println(buckets[i].color);
}
}
}
in.close();
}
}

class MouseBucket {
int weight;
String color;
}

发表于 2016-07-21 10:14:50 回复(0)
这一题难度中等,也难怪通过率很高。
这里主要的问题是map的基本用法不太熟悉,熟悉了基本上就没什么问题了。
不足的是,排序用的冒泡,因为不太知道map应该如何用排序函数,应该还有更高效的方法吧
#include<iostream>
#include<string>
#include<map>
using namespace std;
struct Mouse {
    int weight;
    string color;
};
int main() {
    int n;
    cin >> n;
    map<int, Mouse> mouse;
    for (int i = 1; i <= n; i++) {
        cin >> mouse[i].weight >> mouse[i].color;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (mouse[i].weight < mouse[j].weight) {
                Mouse* temp = new Mouse();
                *temp = mouse[i];
                mouse[i] = mouse[j];
                mouse[j] = *temp;
            }
        }
    }
    map<int, Mouse>::iterator iter;
    for (iter = mouse.begin(); iter != mouse.end(); iter++)
        cout << iter->second.color << endl;
    return 0;
}

发表于 2019-02-07 09:59:41 回复(2)
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct E{
    int weight;
    char color[11];
}buf[101];
bool cmp(E a,E b){
    return a.weight>b.weight;
}
int main(){
    int n,i,j;
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<n;i++){
            scanf("%d%s",&buf[i].weight,buf[i].color);
        }
        sort(buf,buf+n,cmp);
        for(i=0;i<n;i++){
            printf("%s\n",buf[i].color);
        }
    }
}

发表于 2019-01-05 13:04:05 回复(0)
try:
    while True:
        num = int(input())
        mouses = []
        for i in range(num):
            mouses.append(input().split())
            mouses[i][0] = int(mouses[i][0])
        mouses.sort(reverse=True)
        for i in mouses:
            print(i[1])
except Exception:
    pass
编辑于 2018-10-08 22:28:36 回复(0)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 100;

struct Node
{
    int weight;
    string color;
}num[maxn];

bool cmp(Node a, Node b)
{
    return a.weight > b.weight;
}

int main()
{
    
    int n;
    while(cin >> n)
    {
        for(int i = 0; i < n; ++i)
        {
            cin >> num[i].weight >> num[i].color;
        }
        sort(num, num+n, cmp);
        for(int i = 0; i < n; ++i)
        {
            cout << num[i].color << endl;
        }
    }

    return 0;
}


发表于 2016-08-07 15:11:27 回复(0)

python solution:

while True:
    try:
        a, arr = int(input()), []
        for i in range(a):
            arr.append(input().split())
        for i in sorted(arr,key=lambda c:int(c[0]),reverse=True):
            print(i[1])
    except:
        break
发表于 2017-10-17 09:32:41 回复(0)
import java.util.Scanner;

/*
 *	QQ:	825580813(一起来敲代码)
 *	思路:
 *		1, 开辟一个大小为101的String数组
 *		2, 将小白鼠的重量作为下标, 帽子颜色作为数组值
 *		3, 从后往前输出 非null 的数组值
 */
public class Main{
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n;
		while (sc.hasNext()) {
			n = sc.nextInt();
			String[] hatColor = new String[101];
			for (int i = 0; i < n; ++i) {
				hatColor[sc.nextInt()] = sc.next();
			}
			for (int i = hatColor.length - 1; i > 0; --i) {
				if (hatColor[i] != null) {
					System.out.println(hatColor[i]);
				}
			}
		}
		sc.close();
	}
	
}


发表于 2017-06-01 23:52:37 回复(1)
Java 解法,借助TreeMap和Stack
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        TreeMap<Integer, String> map = new TreeMap<>();
        for (int i = 0; i <n; i++) {
            int weight = scanner.nextInt();
            String cap = scanner.next();
            map.put(weight,cap);
        }
        // 默认从小到大遍历,使用Stack进行从大到小遍历
        Stack<String> stack = new Stack<>();
        for (String value : map.values()) {
           stack.push(value);
        }
        while (!stack.isEmpty())
            System.out.println(stack.pop());
    }
}


发表于 2020-03-07 11:38:44 回复(0)
/*使用map<int,string> 数据结构 逆向迭代器遍历 可以简单解决这个问题*/
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  std::map<int, string> m;
  std::map<int, string>::reverse_iterator it;
  int n,tmpi,i;
  string tmps;
  while (cin>>n) {
    
    for ( i = 0; i < n; i++) {
      cin>>tmpi>>tmps;
      m.insert(pair<int,string>(tmpi,tmps));
    }
    
    for(it=m.rbegin();it!=m.rend();it++) {
      
      cout<<it->second<<endl;
    }
  }
  return 0;
}
发表于 2018-07-03 11:43:01 回复(0)
#include<iostream>
#include<string.h>

using namespace std;

int MAX_N = 101;

int main(){
    int n;
    string arr[MAX_N];
    while(cin >> n){
        for(int i = 0; i < MAX_N; i++){
            arr[i] = "#";
        }
        while(n--){
            int weight;
            string color;
            cin >> weight >> color;
            arr[weight] = color;
        }
         for(int i = MAX_N - 1; i >= 0; i--){
            if(arr[i].compare("#")){
                cout << arr[i] <<endl;
            }
        }
    }
    return 0;
}

发表于 2022-02-25 22:33:21 回复(0)
#include<iostream>
#include<algorithm>

using namespace std;

const int MAXN = 100 + 10;

struct mouse{
    int w;
    string color;
};
mouse m[MAXN];

bool cmp(mouse &x, mouse &y){
    return x.w > y.w;
}

int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0;i < n;i++) cin>>m[i].w>>m[i].color;
        sort(m, m + n, cmp);
        for(int i = 0;i < n;i++) cout<<m[i].color<<endl;
    }
    return 0;
}
发表于 2022-01-22 22:41:43 回复(0)
#include<stdio.h>
(737)#include<string.h>
struct Month{//0.搞一个结构体
    int weight;
    char color[10];
}mo[100];
int main()
{
    int n,i,j,t;char tmo[10];
    scanf("%d",&n);//1.输入
    for(i=0;i<n;i++)
        scanf("%d%s",&mo[i].weight,mo[i].color);
    for(i=0;i<n-1;i++)//2.排序
        for(j=0;j<n-1-i;j++)
            if(mo[j].weight<mo[j+1].weight)//从大到小排序
            {//全部交换
                t=mo[j].weight;strcpy(tmo,mo[j].color);
                mo[j].weight=mo[j+1].weight;strcpy(mo[j].color,mo[j+1].color);
                mo[j+1].weight=t;strcpy(mo[j+1].color,tmo);
            }
    for(i=0;i<n;i++)//3.输出
        printf("%s\n",mo[i].color);
}

发表于 2020-03-31 17:43:53 回复(0)
#include<stdio.h>
#include<algorithm>
using namespace std;
struct Baishu{
 int n;
 char color[20];
}baishu[101];
/*
bool cmp(Baishu a,Baishu b){
 return a.n>b.n;
}
*/
void bubbleSort(Baishu a[101],int m){
 for(int i=0;i<m-1;i++){
  for(int j=0;j<m-i-1;j++){
   if(a[j+1].n>a[j].n){
    Baishu temp=a[j+1];
    a[j+1]=a[j];
    a[j]=temp;
   }
  }
 }
}
int main(){
 int s;
 while(scanf("%d",&s)!=EOF){
 for(int i=0;i<s;i++){
 scanf("%d%s",&baishu[i].n,baishu[i].color);
 }
 bubbleSort(baishu,s);
 //sort(baishu,baishu+s,cmp);
 for(int i=0;i<s;i++){
 printf("%s\n",baishu[i].color);
 }
 }
 return 0;
 }

编辑于 2018-05-13 23:32:30 回复(0)
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct rat{
    int weight;
    string color;
};
bool cmp(rat lhs, rat rhs){
    return lhs.weight > rhs.weight;
}
int main(){
    int n;
    while(cin >> n){
        rat str[102];
        for(int i=0; i<n; ++i){
            cin >> str[i].weight >> str[i].color;
        }
        sort(str, str+n,cmp);
        for(int i=0; i<n; ++i){
            cout << str[i].color << endl;
        }
    }
}

编辑于 2024-02-29 22:03:32 回复(0)
#include <iostream>
#include <algorithm>
using namespace std;

struct S
{
    int weight;
    string color;
};

//判断函数
bool Compare(S a,S b)
{
    return a.weight>b.weight;
}

int main()
{
    int n;
    while(cin>>n)
    {
        S a[n];
        for(int i=0;i<n;i++)
            cin>>a[i].weight>>a[i].color;
        sort(a,a+n,Compare);
        for(int i=0;i<n;i++)
            cout<<a[i].color<<endl;
    }
    return 0;
}

发表于 2024-02-21 14:11:46 回复(0)
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

struct Mouse{
    int weight;
    char color[20];
};

bool comp(Mouse lhs,Mouse rhs){
    return lhs.weight >= rhs.weight;
}

int main() {
    int n;
    scanf("%d",&n);
    vector<Mouse> vec(n);
    for(int i = 0; i < n; ++i){
        Mouse mouse;
        scanf("%d%s",&vec[i].weight,&vec[i].color);
    }
    sort(vec.begin(),vec.end(),comp);
    for(int i = 0; i < n; ++i){
        printf("%s\n",vec[i].color);
    }
    return 0;
}

编辑于 2024-01-25 20:57:17 回复(0)
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;

struct mouse
{
    int weight;
    string colour;
    //写数组就不要用构造函数了
};
bool compare(mouse x,mouse y)
{
    return x.weight > y.weight;
}
//mouse[100];
mouse arr[100];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; ++i)
    {
        int w;
        string c;
        scanf("%d",&w);
        cin>>c;
        arr[i].weight = w;
        arr[i].colour = c;
    }
    sort(arr,arr+n,compare);
    for(int i=0; i<n; ++i)
    {
        cout<<arr[i].colour<<endl;
    }
}

简单的结构体+排序即可解决
发表于 2021-04-26 10:05:15 回复(0)
#include <iostream>
#include <stdlib.h>
using namespace std;

struct mouse {
    int weight;
    string color;
}mousee[100];

void quicksort(mouse a[100], int low, int high) {
    if (low < high) {
        int l = low, h = high;
        mouse pivot = a[low];
        while (l < h) {
            while (l < h && a[h].weight > pivot.weight)
                h--;
            a[l].weight = a[h].weight;
            a[l].color = a[h].color;
            while (l < h && a[l].weight < pivot.weight)
                l++;
            a[h].weight = a[l].weight;
            a[h].color = a[l].color;
        }
        a[l].weight = pivot.weight;
        a[l].color = pivot.color;
        quicksort(a, low, l - 1);
        quicksort(a, l + 1, high);
    }
}

int main() {

    int n;
    cin>>n;
    for (int i = 0; i < n; i++) {
        cin >> mousee[i].weight;
        cin >> mousee[i].color;
        //cout << i << mousee[i].color << endl;
    }

    quicksort(mousee, 0, n-1);

    for(int i = n-1;i >=0; i-- ) {
        cout << mousee[i].color << endl;
    }
    return 0;
}
编辑于 2024-03-18 21:57:40 回复(0)