输入:4
20 35 23 40
输出:2
出四份难易度不同试卷,每个考场至少三份试卷a,b,c且满足a<=b<=c,b-a<=10,c-b<=10,每份试卷只能在一个考场,考场数>=1求至少还需要出几份试卷?
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n = in.nextInt();
in.nextLine();
int[] arr = new int[n];
for(int i = 0;i < n;i++){
arr[i] = in.nextInt();
}
int m = 0;
Arrays.sort(arr);
int temp = 0;
for(int i = 0;i < n;){
if(i+2 < n){
temp = diff(arr[i],arr[i+1], arr[i+2]);
if(temp == 0){
i += 3;
}else if(temp == 1){
i += 2;
}else if(temp == 2){
i += 1;
}
m += temp;
}else if(i == n-1){
m += 2;
break;
}else if(i == n-2){
if(arr[n-1] - arr[n - 2] <=20){
m += 1;
break;
}else{
m += 4;
break;
}
}
}
System.out.println(m);
}
}
public static int diff(int p,int q,int m){
int temp1 = q - p;
int temp2 = m - q;
int temp3 = m - p;
int count = 0;
if(temp3 <= 10){
return count;
}else if(temp3 > 10 && temp3 <= 20){
count++;
}else if(temp3 > 20){
if(temp1 <= 20){
count++;
}else if(temp1 > 20){
count += 2;
}
}
return count;
}
}
看到的麻烦给指导一下,谢谢