拼多多服务端第三题 求大佬解惑 只过了10% 气死了

import java.text.DecimalFormat;
import java.util.*;

public class Main2 {
    public void pdd2(int[] nums) {
        DecimalFormat df = new DecimalFormat("0.00");
        Arrays.sort(nums);
        int base = get_count(nums,-1);
        //System.out.println(Arrays.toString(nums));
        int result = 0;
        int last_index = nums.length - 1;
        while(nums[last_index]!=1){
            int count = get_count(nums,last_index);
            result += count*nums[last_index];
            System.out.println(nums[last_index]+"   "+count);
            nums[last_index]--;
            while(last_index - 1>= 0 && nums[last_index] < nums[last_index - 1]){
                my_swap(nums,last_index,last_index - 1);
                last_index--;
            }
            last_index = nums.length - 1;
        }
        result+=1;
        double final_result = result*1.0/base;
        System.out.println(df.format(final_result));
    }
    public int get_count(int[] nums,int except_index){
        int result = 1;
        for (int i = 0; i < nums.length; i++) {
            if(i == except_index)continue;
            result*=nums[i];
        }
        return result;
    }
    public void my_swap(int[] nums,int a,int b){
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
    public static void main(String[] args) {
        Main2 main = new Main2();
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        int[] nums = new int[num];
        while (scanner.hasNextLine()) {
            String[] strs = scanner.nextLine().split(" ");
            for (int i = 0; i < num; i++) {
                nums[i] = Integer.parseInt(strs[i]);
            }
            main.pdd2(nums);
        }
    }
}

#拼多多##笔试题目#
全部评论
就是溢出了。所有的都换成double
点赞 回复 分享
发布于 2019-09-01 18:08
我也是10,气死
点赞 回复 分享
发布于 2019-09-01 18:20
import java.util.Arrays; import java.util.Scanner; public class Main_2 {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         int n = scanner.nextInt();         int[] x = new int[51];         for (int i = 0; i < n; i++) {             int M = scanner.nextInt();             x[M]++;         }         double expection = 0;         double temp2 = 1;         double temp1 = 1;         for (int i = 50; i > 0; i--) {             if (x[i] > 0) {                 temp1 *= Math.pow(i - 1, x[i]) / Math.pow(i, x[i]);                 double pro = temp2 - temp1;                 temp2 = temp1;                 expection += i * pro;                 x[i - 1] += x[i];             }         }         System.out.printf("%.2f", expection);     } }
点赞 回复 分享
发布于 2019-09-01 20:37

相关推荐

02-11 12:20
门头沟学院 Java
面试中的青提很胆小:我不信有比我们学校更逆天的,计算机专业就业第一位是我们学校二餐厅的打印店
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务