利用二分法

小易的英语软件

http://www.nowcoder.com/questionTerminal/f51e08c7642345fb96dd7b29792b0790

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class ScoreToPercent {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int[] scores = new int[n];
            for (int i=0;i<n;i++)
                scores[i]=scanner.nextInt();

            HashMap<Integer, Integer> map = new HashMap<>();
            int[] sort = Arrays.copyOfRange(scores, 0, scores.length);
            Arrays.sort(sort);
            for (int i=0;i<n;i++){
                int left=0;
                int right=n-1;
                while (left<=right){
                    int mid=left+(right-left)/2;
                    if (sort[mid]<=scores[i])
                        left=mid+1;
                    else{
                        right=mid-1;
                    }
                }
               // System.out.println("left:"+left);
                map.put(i,left-1);
            }
            int m = scanner.nextInt();
            for (int i=0;i<m;i++){
                double res = (100.0*map.get(scanner.nextInt() - 1))/ n;
                System.out.println(String.format("%.6f",res));
            }

        }
    }
}
全部评论

相关推荐

11-07 13:31
怀化学院 Java
勇敢牛牛不怕难:又疯一个
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务