编程介的小学生 2019-04-04 11:58 采纳率: 0.4%
浏览 174

一个rank的分类的一个算法的问题,用了C语言的程序的设计怎么求解的

Problem Description
As we all know, after a series of international contests, the leaders are wild about ranking the schools to appraise the development of the ACM of our country. There are a lot of schools attend the contests, and each school has some teams or none, and each team may get some prizes of not. There are three kinds of prizes of the contests: gold, silver and copper, and gold is the best one and silver is better than copper. Now we get the result of all the schools, you should rank them, and print them according to the below rules:

1) We define the ranks between any two schools (or two teams) as the follow rules: firstly we compare the number of gold prizes, and the school is better whose number of gold prizes is larger, and if the numbers of gold prize are the same then compare the silver prizes and then copper prizes. If all the numbers of gold prizes and silver prizes and copper prizes are the same, then we just say the two school (or two teams) are the same good, and their ranks are the same, you have to obey the lexicographic orders when you print them, though.

2) We define the rank number as the following rule: if there are three schools (or three teams), A is as good as B, but better than C. So the rank number of A and B is 1 (the rank number starts from 1), and C is 3, we omit the rank number 2. If more, the rule goes on.

Input
There are T cases come, and the first line contains just one integer T.
In each case of following T ones, there is an integer N indicates that there are N following lines describe the information of the encouragement.
Each line contains fours strings: the name of the school, the name of the team, the kind of the prize ("none" means the team gets no prize), and the contest hosting place. Any string is no longer than fifty characters. T<=10, N <=200, and the school number will not be beyond N, and the team number of each school will not exceed 100.

Output
For each case, firstly you show the number of school, and then show the rank list as the format: the school name, the rank number, and the numbers of gold prize and silver prize and copper prize.
Then you print the teams' information: the team name, the rank number, the numbers of gold prize and silver prize and copper prize.
After printing the rank list, firstly you print the number of the contests, and then you have to print the contest information: the name of hosting place (shown lexicographically), the number of gold prize, and the number of silver prize and copper. You can assume all the data is correct.

Sample Input
1
4
aaa mayday gold nanjing
bbb let's_go silver beijing
ccc how_do_you_do??? none xihua
aaa acm.hdu.edu.cn copper xihua

Sample Output
Case 1:


school number: 3
aaa 1 1 0 1
team number: 2
mayday 1 1 0 0
acm.hdu.edu.cn 2 0 0 1
bbb 2 0 1 0
team number: 1
let's_go 1 0 1 0
ccc 3 0 0 0
team number: 1
how_do_you_do??? 1 0 0 0


contest number: 3
beijing 0 1 0
nanjing 1 0 0
xihua 0 0 1


  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-08 18:24
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这是一个典型的排序问题,可以使用快速排序或归并排序来解决。以下是一个使用C语言实现的快速排序算法:

    #include <stdio.h>
    #include <string.h>
    
    int partition(int arr[], int low, int high)
    {
        int pivot = arr[high];
        int i = (low - 1);
    
        for (int j = low; j <= high - 1; j++)
        {
            // 如果当前元素小于等于pivot,则将它插入到i的位置
            if (arr[j] <= pivot)
            {
                i++;
                char temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    
        char temp = arr[i + 1];
        arr[i + 1] = arr[high];
        arr[high] = temp;
    
        return i + 1;
    }
    
    void quicksort(int arr[], int low, int high)
    {
        if (low < high)
        {
            int pi = partition(arr, low, high);
            quicksort(arr, low, pi - 1);
            quicksort(arr, pi + 1, high);
        }
    }
    

    这个函数接受一个数组、一个起始索引和一个结束索引作为参数,并对数组进行排序。

    然后我们就可以根据题目要求输出结果了:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_SIZE 200
    
    int main()
    {
        int t, n, k, m, s;
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d", &n);
            int *array = malloc(n * sizeof(int));
            for(int i=0;i<n;i++) array[i]=0;
            
            for(int i=0;i<n;i++){
                char str[MAX_SIZE+5];
                scanf("%s %s %s %s", str, str+5, str+5+5, str+5+5+5);
                
                strcpy(array+i, str);
            }
            
            quicksort(array, 0, n-1);
            
            printf("Case %d:\n", i+1);
            
            for(int i=0;i<n;i++){
                printf("%d ", array[i]);
            }
            puts("");
            
            for(int i=0;i<n;i++){
                printf("%d\n", array[i]);
            }
        }
        
        free(array);
        return 0;
    }
    

    这段代码首先读取输入的测试用例数量t,然后对于每个测试用例,读取学校的个数n,接着为每个学校创建一个数组,并读取团队信息。接下来调用quickSort函数对数组进行排序。最后打印出排好序的结果和每所学校的信息。

    评论

报告相同问题?