Gym 100820C(级别排序 **)

题意是说有一些人参加了不同级别的班,级别有 lower,middle,upper 三种,级别可以组合,出现比如 lower upper,middle upper 这种级别,级别的比较是从右往左,如果在一组比较中有的人的组合级别多,就以本组中级别最多的作为参照,其他人的级别要在左边添加 middle 来补到一样多。如果有人的级别是相等的,这些级别相等的人就按照名字的字典序排序。最后将排好序的名字依次输出。

开始本人的做法是将 lower,middle,upper 分别变成 1,2,3,然后从右向左将每个人的级别写成一个十进制的数,用 sort() 排序即可。

但是题中说每行不超过 256 个字符,也就是说级别数量会达到 50 个左右,写成一个数字很明显是存不下的,然后就糊涂了,竟然开始考虑用 4 进制来存,其实这里没有进位,和十进制是一样的长度,而且继续降低进制会反而将数字变长......

经高人指点,恍然大悟,原来可以直接开数组去存每一个数......

此外,在进行字典序排序的时候竟然不知道怎么写,其实可以直接比较 string 的,竟然还手写去连续比较了几位.......

题目代码如下:

  1 //#include <cstdio>
  2 //#include <iostream>
  3 //#include <algorithm>
  4 //using namespace std;
  5 //struct mem
  6 //{
  7 //    string name,al;
  8 //    int cnt,sco,w[1050];
  9 //}stu[1052];
 10 //int n;
 11 //bool cmp(mem a,mem b)
 12 //{
 13 //    if(a.sco != b.sco)
 14 //        return a.sco < b.sco;
 15 //    else if(a.name[0] != b.name[0])
 16 //        return a.name[0] > b.name[0];
 17 //    else if(a.name[1] != b.name[1])
 18 //        return a.name[1] > b.name[1];
 19 //    else if(a.name[2] != b.name[2])
 20 //        return a.name[2] > b.name[2];
 21 //    else if(a.name[3] != b.name[3])
 22 //        return a.name[3] > b.name[3];
 23 //    return a.name[4] > b.name[4];
 24 //}
 25 //int main()
 26 //{
 27 //    int len,big;
 28 //    bool f;
 29 //    scanf("%d",&n);
 30 //    getchar();
 31 //    big = -1000;
 32 //    for(int i = 0;i <n; i++)
 33 //    {
 34 //        getline(cin,stu[i].al);
 35 //        len = (stu[i].al).length();
 36 //        f = true;
 37 //        stu[i].cnt = 0;
 38 //        stu[i].sco = 0;
 39 //        for(int j = 0 ;j < len; j++)
 40 //        {
 41 //            if(stu[i].al[j] == ':') f = false;
 42 //
 43 //            if(f) stu[i].name += stu[i].al[j];
 44 //            else{
 45 //                if(stu[i].al[j] == 'c') break;
 46 //                else if(stu[i].al[j] == 'u')
 47 //                {
 48 //                    stu[i].w[stu[i].cnt++] = 3;
 49 //                    j += 5;
 50 //                }
 51 //                else if(stu[i].al[j] == 'm')
 52 //                {
 53 //                    stu[i].w[stu[i].cnt++] = 2;
 54 //                    j += 6;
 55 //                }
 56 //                else if(stu[i].al[j] == 'o')
 57 //                {
 58 //                    stu[i].w[stu[i].cnt++] = 1;
 59 //                    j += 4;
 60 //                }
 61 //            }
 62 //        }
 63 //        if(stu[i].cnt > big) big = stu[i].cnt;
 64 //    }
 65 //    for(int i = 0 ; i < n;i++)
 66 //    {
 67 //        for(int j = stu[i].cnt-1; j >=0 ; j--)
 68 //            stu[i].sco = stu[i].w[j] + stu[i].sco *4;
 69 //        while(stu[i].cnt < big)
 70 //        {
 71 //            stu[i].sco = 2 + stu[i].sco*4;
 72 //            stu[i].cnt++;
 73 //        }
 74 //    }
 75 //    sort(stu,stu+n,cmp);
 76 //    for(int i = n-1 ; i >= 0; i--)
 77 //        cout << stu[i].name  << endl;
 78 //    return 0;
 79 //}
 80 #include<cstdio>
 81 #include<iostream>
 82 #include<cstring>
 83 #include<algorithm>
 84 using namespace std;
 85 struct node
 86 {
 87     char name[100];
 88     char lever[100];
 89     char sco[300];
 90 } stu[1100];
 91 bool cmp(struct node a,struct node b)
 92 {
 93     if(strcmp(a.sco,b.sco) != 0) return (strcmp(a.sco,b.sco) > 0);
 94     return strcmp(a.name,b.name) < 0;
 95 }
 96 void rev(char *s,int n)
 97 {
 98     for(int i = 0, j = n - 1; i < j; i++, j--)
 99     {
100         char c = s[i];
101         s[i] = s[j];
102         s[j] = c;
103     }
104 }
105 int main()
106 {
107     int cnt,n;
108     scanf("%d",&n);
109     for(int i = 0; i < n; i++)
110     {
111         cnt = 0;
112         scanf("%s",stu[i].name);
113         int str = strlen(stu[i].name);
114         stu[i].name[str-1] = 0;
115         for(int j = 0; j < 260; j++)
116         {
117             if(j == 259) stu[i].sco[j] = 0;
118             else stu[i].sco[j] = '2';
119         }
120         while(1)
121         {
122             scanf("%s",stu[i].lever);
123             if(strcmp("upper",stu[i].lever)==0) stu[i].sco[cnt++]='3';
124             if(strcmp("middle",stu[i].lever)==0) stu[i].sco[cnt++]='2';
125             if(strcmp("lower",stu[i].lever)==0) stu[i].sco[cnt++]='1';
126             if(strcmp("class",stu[i].lever)==0) break;
127         }
128             rev(stu[i].sco,cnt);
129     }
130         sort(stu,stu+n,cmp);
131         for(int i=0; i<n; i++) printf("%s
",stu[i].name);
132     return 0;
133 }
View Code
日后若能有更好的想法,再来完善。 希望看到的大神不吝赐教 orz
原文地址:https://www.cnblogs.com/Taskr212/p/9509510.html