Wil*_*ott 1 java error-handling compiler-errors
我的代码在我的编译器中运行良好,我什至尝试了另外几个在线编译器,但仍然无法找到问题,有人可以帮忙!
题
https://www.codechef.com/JUNE18B/problems/NAICHEF
有一次,在紧张的一天之后,大厨决定放松一下,去他家附近的赌场赌博。他觉得自己很幸运,他几乎将赌上所有的钱。
大厨将在赌场玩的游戏包括两次掷 N 个面的骰子。骰子的每个面上都有一个数字(这些数字不一定是不同的)。为了获胜,厨师必须在第一次掷骰子时获得数字 A,在第二次掷骰子时获得数字 B。
兴奋的观众想知道厨师赢得比赛的概率。你能帮他们找到那个号码吗?假设厨师在每次掷骰子时以相同的概率获得骰子的每个面,并且掷骰子是相互独立的。
我的提交
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));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
首先,您应该使用 double 而不是 float (精度很重要)!
其次,你应该更新你的状态条件,因为你只考虑了第一个子任务(T小于10,N小于100),这只会给你20分!第二个子任务(奖励 80 分)需要一个小于 70 的 T 和一个小于 1000 的 N。
最后,代码的问题来自于更新 pa & pb 的条件,您使用:
Integer.parseInt(f1) == A[0] // same for B[0]
Run Code Online (Sandbox Code Playgroud)
代替
Integer.parseInt(f1) == A[t] // same for B[t]
Run Code Online (Sandbox Code Playgroud)
下面是完整的代码和提交结果
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)