Sung's Placement Test

分班规则:

  1)如果分数>=T2, 那么该学生会被分配到A班;

  2)如果分数>=T1&&<T2,那么该学生会被分配到B班;

  3)如果分数<T1,则该学生会被分配到C班。

  4)每班的最小人数不能低于Kmin(>=Kmin),最大人数不能大于Kmax(<=Kmax)

  现在,在T1,T2没有给定的情况下,给出要分班的学生的分数,要求按照分配规则来分班,并求出人数最多的班与人数最少的班的人数差异,如果有多种可能,则求其最小值,若无法按照规则分配,则输出-1。

  输入要求:

  1)第一行,输入T,表示有T个测试案例

  2)第二行开始,输入每个测试案例,其中每个案例,第一行输入三个数,N代表学生的人数(1-1000), Kmin代表每个班人数的下限,Kmax代表每个班的人数上限

  3)输入学生的分数(1-100)

  输出要求:

  输出(#+test_cast+" "+result)

 

  思路:

  题目中,分班的界定T1, T2并未知,因此可能有多种可能。我们可以找出学生中的最高分与最低分,以此为界限,不断去找可能的T1, T2,并检查每班的人数是否符合要求,如果符合,则与当前的result比较,找出更贴合题目要求的值。其中找出最高分与最低分,一种很直观的做法就是把分数(int[] scores)排序,那么scores[0]即为最低分,scores[N-1]即为最高分。另一种做法就是,先设定一个特别的最高分与最低分,在输入学生分数的时候,不断与当前的最高分与最低分作比较,并适时更新。

 

import java.util.Scanner;
 
public class Solution {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for(int tc = 1; tc <= T; tc++) {
            int N = in.nextInt();
            int min = in.nextInt();
            int max = in.nextInt();
            int[] scores = new int[1000];
            int minScore = 100;
            int maxScore = 1;
            for(int i = 0; i < N; i++) {
                scores[i] = in.nextInt();
                if(minScore > scores[i]) {
                    minScore = scores[i];
                }
                if(maxScore < scores[i]) {
                    maxScore = scores[i];
                }
            }
            int res = 1000;
            for(int bottom = minScore; bottom < maxScore; bottom++) {
                for(int top = maxScore; top > bottom; top--) {
                    int[] count = new int[3];
                    boolean tag = true;
                    for(int k = 0; k < N; k++) {
                        if(scores[k] >= top) {
                            count[0]++; //A class
                            if(count[0] > max){
                                tag = false;
                                break;
                            }
                        }else if(scores[k] < bottom) {
                            count[2]++; //C class
                            if(count[2] > max){
                                tag = false;
                                break;
                            }
                        }else {
                            count[1]++; //B class
                            if(count[1] > max){
                                tag = false;
                                break;
                            }
                        }
                    }
                     
                    for(int a = 0; a < 3; a++) {
                        if(count[a] < min) {
                            tag = false;
                            break;
                        }
                    }
                    if(tag) {
                        sort(count, 3);
                        int temp = count[2]-count[0];
                        if(res > temp) {
                            res = temp;
                        }
                         
                    }
                }
            }
            if(res == 1000) {
                res = -1;
            }
            System.out.println("#" + tc + " " + res);
        }
    }
     
    public static void sort(int[] array, int len) {
        for(int i = 0; i < len; i++) {
            for(int j = i+1; j < len; j++) {
                if(array[i] > array[j]) {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
     
}

 

posted on 2020-04-05 12:31  Jain_Shaw  阅读(119)  评论(0编辑  收藏  举报

导航