[Algorithm] entry on the structure of the sort function with the use of custom rules cmp

Ptal025: 

#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct testee{
    char id[20];
    int l_num;
    int score;
    int local_rank;
}stu[30010];

bool cmp(testee a,testee b)
{
    if(a.score==b.score)return strcmp(a.id,b.id)<0;
    else return a.score>b.score;
}
int main()
{
    int N,k,num=0;
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
    {
        scanf("%d",&k);
        for(int j=0;j<k;j++)
        {
            scanf("%s%d",stu[num].id,&stu[num].score);
            stu[num].l_num=i;
            num++;//总数
        }
        sort(stu+num-k,stu+num,cmp);
        stu[num-k].local_rank=1;
        for(int p=num-k+1;p<num;p++)
        {
            if(stu[p].score==stu[p-1].score){
                     stu[p].local_rank=stu[p-1].local_rank;
            }
            else {
                     stu[p].local_rank=p+1-(num-k);
            }
        }
    }
    sort(stu,stu+num,cmp);
    int final_rank=1;
        printf("%d\n",num);
        for(int p=0;p<num;p++)
        {
            if(p>0&&stu[p].score!=stu[p-1].score)
           final_rank=p+1;
            printf("%s %d %d %d",stu[p].id,final_rank,stu[p].l_num,stu[p].local_rank);
            if(p!=num-1)printf("\n");
        }

    return 0;
}

note:

Use a structure represented students can, inside the member variables only need to set three of the four topics need to output in the (final ranking can not be placed inside), and then add a score input, you can.

The main idea:

+ Sort function by using a structure, since the definition of the structure of the comparator cmp rules

(Do not forget header <algorithm> STD namespace and the using , this is a function in C ++)

Rules of comparison: According to the meaning of the questions can, in accordance with the same score student number in ascending order, not in accordance with the number of students score in ascending order.

Due to the output rankings, so drained order also given rankings (because there may appear the same rank). Note that although the same rank ranking as the next person but not from the same ranking they began, based on how many people in front of himself. Represents a specific implementation of the ranking for loop loop variable (may be i + 1), if the same, equal to the previous position, is then equal to where their different index +1.

When saving the input data, the need for two-story loop, outer loop represents a few classes, class inside the inner loop represents a number of people, one by one, and then enter their information.

When the inner end of each cycle, it has been possible in accordance with the subject requirements, calculated within each class rank, and this time is best to minimize the extra set of variables to represent their relationship, through the inner for loop variables, and the number of personnel input, as well as all the population in counter-defined before the start of the cycle, which can represent a time value from the beginning to the end to which value is sorted.

For example each class sort is stu + num-k from the beginning to the end stu + num

 

Also note that in addition, after the end of the two-story cycle, all student information exists in the structure of the array of students, the end position index is num-1.

And then sort results sort title requirements of the final output, this can not survive, because he is traversing all student data stored in the array, and outputs are the same as required for a loop.

So you can get those things changed side output requirements of the subject.

Also note that, and that sort of inner layer of each class, like, sort given to re-complete ranking, not directly in accordance with the array index, because the same person may appear rankings.

The point to note here is that I initially stuck for a long time.

You should still make their own little problems, unskilled thinking clearly.

Note that the above point in time should write naturally found.

Just remember the key is to use an array, sort, custom rules just fine.

Published 68 original articles · won praise 20 · views 6881

Guess you like

Origin blog.csdn.net/qq_43633973/article/details/102668661