Emily Ye
@EmilyYe4
Discord - Iamety1318#8812
Repls
Community
0
I need a simpler version
Quick, i need a simple version of this code:
import java.util.*;
import java.io.*;
public class Main {
final public static String[] COWS = {"Beatrice", "Belinda", "Bella", "Bessie", "Betsy", "Blue", "Buttercup", "Sue"};
public static int n;
public static int list;
public static void main(String[] args) throws Exception {
HashMap map = new HashMap();
for (int i=0; i<COWS.length; i++) map.put(COWS[i], i);
Scanner stdin = new Scanner(new File("lineup.in"));
n = stdin.nextInt();
list = new intn;
for (int i=0; i<n; i++) {
listi = map.get(stdin.next());
System.out.println(listi);
for (int j=0; j<4; j++) stdin.next();
listi = map.get(stdin.next());
System.out.println(listi);
}
int[] perm = new int[COWS.length];
boolean[] used = new boolean[COWS.length];
go(perm, used, 0);
PrintWriter out = new PrintWriter(new FileWriter("lineup.out"));
for (int i=0; i<COWS.length; i++)
out.println(COWS[perm[i]]);
out.close();
stdin.close();
}
public static boolean go(int[] perm, boolean[] used, int k) {
if (k == perm.length) return valid(perm);
for (int i=0; i<perm.length; i++) {
if (!used[i]) {
perm[k] = i;
used[i] = true;
boolean tmp = go(perm, used, k+1);
if (tmp) return true;
used[i] = false;
}
}
return false;
}
public static boolean valid(int[] perm) {
int[] indexOf = new int[perm.length];
for (int i=0; i<perm.length; i++)
indexOf[perm[i]] = i;
for (int i=0; i<n; i++)
if (Math.abs(indexOflist[i]-indexOflist[i]) != 1)
return false;
return true;
}
}0
1
0
0
Question
My java teacher gave me this question-
The following program returns the right result for test data 1 and 2 --
however wrong result for test data 3 , 4 ....
Try to test on more data, find the bug and fix it----
public class Main
{
public static int comp(int a, int b) {
if(a >= b)
return 1;
else
return -1;
}
public static void main(String[] args) {
int k = 2;
int N = 20;
int consistent = 0;
int P = { {11, 16, 4, 17, 12, 18, 6, 3, 19, 14, 1, 8, 13, 9, 2, 20, 5, 15, 10, 7},
{4, 20, 18, 17, 14, 11, 19, 3, 10, 16, 6, 8, 13, 9, 2, 12, 5, 7, 1, 15} };
//int P = { {7, 4, 5, 1, 9, 10, 11, 15, 14, 18, 13, 19, 16, 2, 6, 17, 8, 12, 20, 3},
// {7, 2, 14, 1, 20, 15, 8, 6, 5, 18, 13, 19, 16, 10, 4, 17, 3, 12, 9, 11},
// {13, 2, 14, 1, 9, 19, 11, 18, 5, 15, 7, 6, 12, 4, 20, 17, 3, 16, 10, 8},
// {18, 2, 17, 9, 19, 6, 11, 15, 5, 7, 13, 1, 16, 4, 10, 14, 3, 20, 12, 8},
// {7, 2, 14, 1, 12, 11, 15, 6, 5, 18, 9, 19, 16, 4, 10, 17, 3, 13, 20, 8} };
for(int i = 0; i < N-1; i++) {
for(int j = i+1; j < N; j++) {
int sum = 0;
for(int r = 0; r < k; r++) {
sum += comp(Pr, Pr);
}
if (sum == k || sum == -2 )
consistent=consistent+1;
System.out.println("cow " + i + " and cow " + j + " is consistent");
}
}
System.out.println("The number of consistent pairs are: " + consistent);
}
}
Can anyone help me?2
1
0
5
Snake KaboomIts a simple Snake game in Kaboom.js
530
2
5
0
Word&Letter CounterA Code That Couts Words And Letters/Numbers
2
0
0
0
CalculatorA Calculator In Java!
24
0
0
A code that counts how many words and letters in a sentence or word you type. You can also change the background and text colors.
Java
A Calculator In Java(and mind I have Grammarly on and it's changing everything I write. kinda annoying)
Java