多案例输入,每个案例的输入第一行为一个整数N,表示小白鼠的数目。 下面有N行,每行是一只白鼠的信息。第一个为不大于100的正整数,表示白鼠的重量,;第二个为字符串,表示白鼠的帽子颜色,字符串长度不超过10个字符。 注意:白鼠的重量各不相同。
每个案例按照白鼠的重量从大到小的顺序输出白鼠的帽子颜色。
3 30 red 50 blue 40 green
blue green red
/* * @Author: Spring Breeze * @Date: 2021-06-29 19:48:16 * @FilePath: \algorithm\test.c * @LastEditTime: 2022-03-22 19:00:23 */ #include <stdio.h> #include <string.h> typedef struct { int count; char color[20]; } Rat; // 冒泡排序 int main() { int num, i = 0; scanf("%d", &num); Rat rats[num]; while (scanf("%d %s", &rats[i].count, &rats[i].color) != EOF && i < num) i++; for (int m = 0; m < num; m++) for (int n = 1; n < num - m; n++) if (rats[n].count > rats[n - 1].count) { int count = rats[n - 1].count; char color[20]; strcpy(color, rats[n - 1].color); rats[n - 1].count = rats[n].count; strcpy(rats[n - 1].color, rats[n].color); rats[n].count = count; strcpy(rats[n].color, color); } for (int i = 0; i < num; i++) printf("%s\n", rats[i].color); return 0; }
#include<stdio.h> #include<stdlib.h> typedef struct { int weight; char color[10]; }mouse; int cmp(const void *a,const void *b) { mouse c=*((mouse *)a); mouse d=*((mouse *)b); return d.weight-c.weight; } int main() { mouse m[101]; int n,i; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d%s",&m[i].weight,m[i].color); qsort(m,n,sizeof(m[0]),cmp); for(i=0;i<n;i++) printf("%s\n",m[i].color); return 0; }
#include <stdio.h> #include <stdlib.h> typedef struct Mouse { int wight; char color[11]; }mouse; int n; mouse m[101]; int cmp(const void* lhs, const void* rhs) { return((mouse*)rhs)->wight - ((mouse*)lhs)->wight; } int main() { while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d %s", &m[i].wight, m[i].color); } qsort(m, n, sizeof(mouse), cmp); for (int i = 0; i < n; i++) { printf("%s\n", m[i].color); } } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct all{ int weight; char colour[10]; }all; int n; int cmp(const void *a,const void *b){ return (*(struct all *)b).weight > (*(struct all *)a).weight ? 1 : -1; } int main(){ while(scanf("%d\n",&n)!=EOF){ all str[n]; for(int i=0;i<n;i++){ scanf("%d %s\n",&str[i].weight,&str[i].colour); } qsort(str,n,sizeof(str[0]),cmp); for(int i=0;i<n;i++){ printf("%s\n",str[i].colour); } } return 0; }
#include<stdio.h> typedef struct{ int weight; char color[10]; }mouse; int main(){ int n,i,tem,j; mouse a[100],temp;; while(scanf("%d\n",&n)!=EOF){ i=n; while(i){ scanf("%d %s",&a[i-1].weight,a[i-1].color); i--; } for(i=0;i<n;i++){ tem=i; for(j=i+1;j<n;j++){ if(a[j].weight>a[tem].weight) tem=j; } temp=a[i]; a[i]=a[tem]; a[tem]=temp; } for(i=0;i<n;i++) printf("%s\n",a[i].color); } }