多案例输入,每个案例的输入第一行为一个整数N,表示小白鼠的数目。 下面有N行,每行是一只白鼠的信息。第一个为不大于100的正整数,表示白鼠的重量,;第二个为字符串,表示白鼠的帽子颜色,字符串长度不超过10个字符。 注意:白鼠的重量各不相同。
每个案例按照白鼠的重量从大到小的顺序输出白鼠的帽子颜色。
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; }
#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; }
#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; }
#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); } } }
#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; }
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(); } }
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()); } }
#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; }
#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; }
#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); }
#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; }
#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; } } }
#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; }
#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; } }