我最后这个过了83,仍然不懂发生了什么 import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class beike01 {     public static void main(String[] args){         Scanner scanner = new Scanner(System.in);         int num = scanner.nextInt();         List<Integer> candy = new ArrayList<>();         for(int i=0; i<num; i++){             candy.add(scanner.nextInt());         }         candy.sort(new Comparator<Integer>() {             @Override             public int compare(Integer o1, Integer o2) {                 return o1-o2;             }         });         int result = minCount(candy);         System.out.println(result);     }     public static int minCount(List<Integer> candy){         int result = 0;         if(candy.size() == 1) return 1;         while(candy.size()>1){             int temp = candy.get(0);             int count = 0;             for(int i=0; i<candy.size(); i++){                 if(candy.get(i) != temp){                     break;                 }else{                     count++;                 }             }             for(int i=0; i<count; i++) candy.remove(0);             for(int i=0; i<count/2; i++) candy.add(0, temp+1);             result += count%2;         }         return result + candy.size();     } }
点赞 评论

相关推荐

06-03 15:32
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务