首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我的代码在codechef中给出了错误的答案

我的代码在codechef中给出了错误的答案
EN

Stack Overflow用户
提问于 2018-06-03 20:26:06
回答 2查看 957关注 0票数 1

我的代码在我的编译器中运行良好,我甚至尝试了其他几个在线编译器,但仍然无法找到问题,有人能帮助我吗!

问题

https://www.codechef.com/JUNE18B/problems/NAICHEF

有一次,在紧张的一天之后,厨师决定放松下来,去他家附近的赌场赌博。他觉得自己很幸运,几乎要把他所有的钱都押上。

Chef要在赌场玩的游戏包括两次抛出N张面孔的骰子。在骰子的每个面上都写有一个数字(这些数字不一定是不同的)。为了获胜,厨师必须在第一次掷骰子时得到数字A,在第二次掷骰子时得到数字B。

兴奋的观众想知道厨师赢得比赛的概率。你能帮他们找到那个号码吗?假设Chef在每次掷硬币时以相同的概率获得骰子的每一面,并且掷硬币是相互独立的。

我的提交

import static java.lang.System.exit;
import java.util.*;
import java.lang.*;

/**
 *
 * @author williamscott
 */
public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        boolean status = true;

        int T = Integer.parseInt(in.nextLine());

        //Original Constraint
        if (T < 1 || T > 10) {
//            System.out.println("Please follow original constraint for T");
//            exit(0);
            status = false;
        }

        int N[] = new int[T], A[] = new int[T], B[] = new int[T];
        float Probability[] = new float[T];

        for (int t = 0; t < T; t++) {

            String[] input = in.nextLine().split(" ");

            N[t] = Integer.parseInt(input[0]);
            A[t] = Integer.parseInt(input[1]);
            B[t] = Integer.parseInt(input[2]);

            if (N[t] < 1 || N[t] > 100) {
//                System.out.println("Please follow original constraint for N");
//                exit(0);
                status = false;
            }

            if (A[t] < 1 || A[t] > N[t]) {
//                System.out.println("Please follow original constraint for A");
//                exit(0);
                status = false;

            }

            if (B[t] < 1 || B[t] > N[t]) {
//                System.out.println("Please follow original constraint for B");
//                exit(0);
                status = false;
            }

            float pn, pa = 0, pb = 0;

            String[] f = in.nextLine().split(" ");
            pn = f.length;

            if (pn != N[t]) {
//                System.out.println("Inputs Invalid");
//                exit(0);
                status = false;
            }

            for (String f1 : f) {

                if (Integer.parseInt(f1) < 1 || Integer.parseInt(f1) > N[t]) {
//                    System.out.println("Please follow original constraint for x (input)");
//                    exit(0);
                    status = false;
                }

                if (Integer.parseInt(f1) == A[0]) {
                    pa++;
                }
                if (Integer.parseInt(f1) == B[0]) {
                    pb++;
                }
            }

            Probability[t] = (pa / pn) * (pb / pn);
        }

        if (status) {
            for (float d : Probability) {
                System.out.println(String.format("%.10f", d));
            }
        }

    }

}

错误:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-03 22:25:48

首先,你应该使用double而不是float (精度很重要)!

其次,你应该更新你的状态条件,因为你只考虑了第一个子任务(T小于10,N小于100),这将只给你20分!第二个子任务(奖励80分)的T小于70,N小于1000。

最后,代码的问题来自更新pa & pb的条件,您可以使用:

    Integer.parseInt(f1) == A[0]  // same for B[0]

而不是

    Integer.parseInt(f1) == A[t]  // same for B[t]

下面是完整的代码和提交结果

    import java.util.*;
    import java.lang.*;

    /**
     *
     * @author aoubidar
     */
    public class Main {

        public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            // number of test cases
            int T = Integer.parseInt(in.nextLine());


            int[] N = new int[T];
            int[] A = new int[T];
            int[] B = new int[T];

            double[] Probability = new double[T];

            for (int t = 0; t < T; t++) {

                String[] input = in.nextLine().split(" ");

                N[t] = Integer.parseInt(input[0]);
                A[t] = Integer.parseInt(input[1]);
                B[t] = Integer.parseInt(input[2]);

                int total, pa = 0, pb = 0 ;

                String[] faces = in.nextLine().split(" ");
                total = faces.length;

                for (String f : faces) {

                    if (Integer.parseInt(f) == A[t]) {
                        pa++;
                    }
                    if (Integer.parseInt(f) == B[t]) {
                        pb++;
                    }
                }

                double pn = (double) (total * total);

                Probability[t] = (pa * pb) / pn ;
            }

            for (double d : Probability) {
                System.out.println(d);
            }


        }

    } 

提交成功:

票数 5
EN

Stack Overflow用户

发布于 2018-06-03 21:35:26

  1. Never使用==!=比较浮点数。数字计算机不能以绝对精度表示浮点数,因此当double工作时,这些测试通常使用float。使用precision.
  2. Leave的好处很少,损失很大,只在需要的时候才转换为int,这里使用calculations
  3. Don't时转换为double会使代码过于复杂,还可以使用int方法来帮助简化。例如,不需要使用数组。上面提到的约束可能不需要在您的程序中进行测试,而是假设它们是真的。
  4. 使用符合Java命名标准的变量名,而且这是有意义的。

例如:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // get number of trys
        String line = scanner.nextLine();
        int trys = Integer.parseInt(line.trim());
        for (int i = 0; i < trys; i++) {
            // for each try, calc probability
            double probability = processTry(scanner);
            System.out.println(probability);
        }
        scanner.close();
    }

    private static double processTry(Scanner scanner) {
        String line;
        // get first line
        line = scanner.nextLine();

        // use Scanner to get ints from line
        Scanner lineScan = new Scanner(line);

        //number of faces
        int numberOfFaces = lineScan.nextInt();
        int a = lineScan.nextInt();
        int b = lineScan.nextInt();
        lineScan.close();

        // scanner to get face values
        line = scanner.nextLine();
        lineScan = new Scanner(line);

        // count of how many faces match a and b values
        int aMatch = 0;
        int bMatch = 0;
        for (int i = 0; i < numberOfFaces; i++) {
            int face = lineScan.nextInt();
            if (a == face) {
                aMatch++;
            } 
            if (b == face) {
                bMatch++;
            }
        }
        lineScan.close();

        // only cast to double when need for calc
        double probability = ((double) (aMatch * bMatch) / (numberOfFaces * numberOfFaces));
        return probability;
    }
}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50666406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档