Gym 100820C(级别排序 **)

时间:2023-02-11 20:40:36

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

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

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

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

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

题目代码如下:

 //#include <cstdio>
//#include <iostream>
//#include <algorithm>
//using namespace std;
//struct mem
//{
// string name,al;
// int cnt,sco,w[1050];
//}stu[1052];
//int n;
//bool cmp(mem a,mem b)
//{
// if(a.sco != b.sco)
// return a.sco < b.sco;
// else if(a.name[0] != b.name[0])
// return a.name[0] > b.name[0];
// else if(a.name[1] != b.name[1])
// return a.name[1] > b.name[1];
// else if(a.name[2] != b.name[2])
// return a.name[2] > b.name[2];
// else if(a.name[3] != b.name[3])
// return a.name[3] > b.name[3];
// return a.name[4] > b.name[4];
//}
//int main()
//{
// int len,big;
// bool f;
// scanf("%d",&n);
// getchar();
// big = -1000;
// for(int i = 0;i <n; i++)
// {
// getline(cin,stu[i].al);
// len = (stu[i].al).length();
// f = true;
// stu[i].cnt = 0;
// stu[i].sco = 0;
// for(int j = 0 ;j < len; j++)
// {
// if(stu[i].al[j] == ':') f = false;
//
// if(f) stu[i].name += stu[i].al[j];
// else{
// if(stu[i].al[j] == 'c') break;
// else if(stu[i].al[j] == 'u')
// {
// stu[i].w[stu[i].cnt++] = 3;
// j += 5;
// }
// else if(stu[i].al[j] == 'm')
// {
// stu[i].w[stu[i].cnt++] = 2;
// j += 6;
// }
// else if(stu[i].al[j] == 'o')
// {
// stu[i].w[stu[i].cnt++] = 1;
// j += 4;
// }
// }
// }
// if(stu[i].cnt > big) big = stu[i].cnt;
// }
// for(int i = 0 ; i < n;i++)
// {
// for(int j = stu[i].cnt-1; j >=0 ; j--)
// stu[i].sco = stu[i].w[j] + stu[i].sco *4;
// while(stu[i].cnt < big)
// {
// stu[i].sco = 2 + stu[i].sco*4;
// stu[i].cnt++;
// }
// }
// sort(stu,stu+n,cmp);
// for(int i = n-1 ; i >= 0; i--)
// cout << stu[i].name << endl;
// return 0;
//}
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
char name[];
char lever[];
char sco[];
} stu[];
bool cmp(struct node a,struct node b)
{
if(strcmp(a.sco,b.sco) != ) return (strcmp(a.sco,b.sco) > );
return strcmp(a.name,b.name) < ;
}
void rev(char *s,int n)
{
for(int i = , j = n - ; i < j; i++, j--)
{
char c = s[i];
s[i] = s[j];
s[j] = c;
}
}
int main()
{
int cnt,n;
scanf("%d",&n);
for(int i = ; i < n; i++)
{
cnt = ;
scanf("%s",stu[i].name);
int str = strlen(stu[i].name);
stu[i].name[str-] = ;
for(int j = ; j < ; j++)
{
if(j == ) stu[i].sco[j] = ;
else stu[i].sco[j] = '';
}
while()
{
scanf("%s",stu[i].lever);
if(strcmp("upper",stu[i].lever)==) stu[i].sco[cnt++]='';
if(strcmp("middle",stu[i].lever)==) stu[i].sco[cnt++]='';
if(strcmp("lower",stu[i].lever)==) stu[i].sco[cnt++]='';
if(strcmp("class",stu[i].lever)==) break;
}
rev(stu[i].sco,cnt);
}
sort(stu,stu+n,cmp);
for(int i=; i<n; i++) printf("%s\n",stu[i].name);
return ;
}