1、http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2714&cid=1199
2、题目:
下沉的船
一艘船很不幸的撞上了暗礁,船长组织大家上救生艇,而且船长决定女人(woman)和小孩(child)先上船,然后其次是男人(man),最后是船长(captain)。给出原来的顺序,通过编程得到按船长要求排列后的顺序。
身份优先级: woman = child > man > captain.
注意:如果两者的身份优先级一样,请按原来的序列的顺序决定,原序列中在前面的人,依然在前面(具体请看样例)
6 Jack captain Alice woman Charlie man Teddy woman Bob child Julia woman
Alice Teddy Bob Julia Charlie Jack
3、AC代码:
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; struct node { char name[15]; char job[15]; int rank; int idx; }a[105]; //判断下标的大小,sort不是自动判断 int cmp(node a,node b) { if(a.rank==b.rank) return a.idx<b.idx; return a.rank<b.rank; } int main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { cin>>a[i].name>>a[i].job; a[i].idx=i; if(strcmp(a[i].job,"woman")==0) a[i].rank=1; else if(strcmp(a[i].job,"child")==0) a[i].rank=1; else if(strcmp(a[i].job,"man")==0) a[i].rank=2; else if(strcmp(a[i].job,"captain")==0) a[i].rank=3; } // for(int i=0;i<n;i++) // printf("%s %d %d\n",a[i].name,a[i].rank,a[i].idx); sort(a,a+n,cmp); for(int i=0;i<n;i++) printf("%s\n",a[i].name); } return 0; } /* 3 a child b woman c child */
也可以直接打印,AC
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; struct node { char name[15]; char rank[15]; }a[105]; int main() { int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%s%s",a[i].name,a[i].rank); for(int i=0;i<n;i++) { if(strcmp(a[i].rank,"woman")==0 || strcmp(a[i].rank,"child")==0) printf("%s\n",a[i].name); } for(int i=0;i<n;i++) { if(strcmp(a[i].rank,"man")==0) printf("%s\n",a[i].name); } for(int i=0;i<n;i++) { if(strcmp(a[i].rank,"captain")==0) printf("%s\n",a[i].name); } } return 0; }