Description
In his memoir So, Anyway. . ., comedian John Cleese writes of the class di erence between his father(who was "middle-middle-middle-lower-middle class") and his mother (who was "upper-upper-lower-middle class"). These fine distinctions between classes tend to confuse American readers, so you are to write a program to sort a group of people by their classes to show the true distinctions.
There are three main classes: upper, middle, and lower. Obviously, upper class is the highest,and lower class is the lowest. But there can be distinctions within a class, so upper-upper is ahigher class than middle-upper, which is higher than lower-upper. However, all of the upper classes(upper-upper, middle-upper, and lower-upper) are higher than any of the middle classes.
Within a class like middle-upper, there can be further distinctions as well, leading to classes like lower-middle-upper-middle-upper. When comparing classes, once you've reached the lowest level of detail, you should assume that all further classes are the equivalent to the middle level of the previous level of detail. So upper class and middle-upper class are equivalent, as are middle-middle-lower-middle and lower-middle.
Input
The rst line of input containsn(1<=n<=1 000), the number of names to follow. Each of the followingnlines contains the name of a person (a sequence of 1 or more lowercase letters `a'-`z'),a colon, a space, and then the class of the person. The class of the person will include one or more modifiers and then the word class. The colon, modi ers, and the wordclasswill be separatedfrom each other by single spaces. All modifiers are one of upper,middle, or lower. It is guaranteed that the input is well-formed. Additionally, no two people have the same name. Input lines are no longer than 256 characters.
Output
Print the n names, each on a single line, from highest to lowest class. If two people have equivalent classes, they should be listed in alphabetical order by name.
Sample
//Sample Input 5 mom: upper upper lower middle class dad: middle middle lower middle class queenelizabeth: upper upper class chair: lower lower class unclebob: middle lower middle class 10 rich: lower upper class mona: upper upper class dave: middle lower class charles: middle class tom: middle class william: lower middle class carl: lower class violet: middle class frank: lower class mary: upper class //Sample Output queenelizabeth mom dad unclebob chair mona mary rich charles tom violet william carl dave frank
题意:
已知班级有上中下之分,班级里又有上中下之分,以此类推。现在给出多个人以及他们所在的班级,要求按照从上到下的顺序给他们排序。
给定的所在班级的顺序为从后到前排列,例如 upper lower class是低于lower upper class(先比较class之前的,以此往前推)。
如果没有排列,默认为中等,例如upper class是高于lower upper class(upper class相当于middle upper class)。
如果两个班级相等,按照姓名的字典序排序。
思路:
简单起见,可以将upper标记为3,middle标记为2,lower标记为1,数组初始化为2,方便用strcmp排序。
从前往后便利数组的时候,最后要将得到的标记的数组翻转一下。
代码:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> #include<iostream> struct node { char name[100]; char lever[100]; char le[300]; } z[1100]; using namespace std; bool cmp(struct node a,struct node b) { if(strcmp(a.le,b.le)==0)//如果标记的数组相等 return strcmp(a.name,b.name)<0;//按照姓名排序 else return (strcmp(a.le,b.le)>0);//否则按照标记的排序 } void Reverse(char *s,int n)//字符串翻转 { for(int i=0,j=n-1; i<j; i++,j--) { char c=s[i]; s[i]=s[j]; s[j]=c; } } int main() { int n ; int logo=0; int flag=0; while(~scanf("%d",&n)) { for(int i=0; i<n; i++) { logo=0; flag=0; scanf("%s",z[i].name); int str = strlen(z[i].name); z[i].name[str-1] = 0;//去掉冒号 for(int j=0; j<300; j++) { if(j==299) z[i].le[j]=0; else z[i].le[j]='2'; } while(1) { flag++; scanf("%s",z[i].lever); if(strcmp("upper",z[i].lever)==0) z[i].le[logo++]='3'; if(strcmp("middle",z[i].lever)==0) z[i].le[logo++]='2'; if(strcmp("lower",z[i].lever)==0) z[i].le[logo++]='1'; if(strcmp("class",z[i].lever)==0) break; } Reverse(z[i].le,flag-1); } sort(z,z+n,cmp); for(int i=0; i<n; i++) { printf("%s",z[i].name); printf("\n"); } } }