您的答案為: 1A0B 正確答案為: 1A1B
我測試過很多組測資都是正確的... 實在不懂這個測資到底是什麼#@#
有人也遇到一樣的嗎?@@
我的程式碼:
import java.util.Scanner;
public class Test {
static String[] nAnB(int[][] data, int[] answer, int n) {
String[] result = new String[n];
boolean[] checkedA = new boolean[4];
int[] checkedB = new int[4];
int[] codeA = new int[n], codeB = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
// check A
if (data[i][j] == answer[j]) {
if (checkedB[j] > 0) { codeB[i] -= checkedB[j]; checkedB[j] = 0; }
codeA[i]++; checkedA[j] = true; continue;
}
// check B
for (int k = 0; k < 4; k++) {
if (!checkedA[k] && checkedB[k] == 0) {
if (data[i][j] == answer[k]) { codeB[i]++; checkedB[k]++; break; }
}
}
}
for (int k = 0; k < 4; k++) { checkedA[k] = false; checkedB[k] = 0; }
result[i] = codeA[i] + "A" + codeB[i] + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] answer = new int[4];
while (scanner.hasNext()) {
for (int i = 0; i < 4; i++) answer[i] = scanner.nextInt();
int n = scanner.nextInt();
int[][] data = new int[n][4];
String[] result = new String[n];
for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) data[i][j] = scanner.nextInt();
result = nAnB(data, answer, n);
for (int i = 0; i < n; i++) System.out.println(result[i]);
}
}
}
您的答案為: 1A0B 正確答案為: 1A1B
我測試過很多組測資都是正確的... 實在不懂這個測資到底是什麼#@#
有人也遇到一樣的嗎?@@
我的程式碼:
import java.util.Scanner;
public class Test {
static String[] nAnB(int[][] data, int[] answer, int n) {
String[] result = new String[n];
boolean[] checkedA = new boolean[4];
int[] checkedB = new int[4];
int[] codeA = new int[n], codeB = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
// check A
if (data[i][j] == answer[j]) {
if (checkedB[j] > 0) { codeB[i] -= checkedB[j]; checkedB[j] = 0; }
codeA[i]++; checkedA[j] = true; continue;
}
// check B
for (int k = 0; k < 4; k++) {
if (!checkedA[k] && checkedB[k] == 0) {
if (data[i][j] == answer[k]) { codeB[i]++; checkedB[k]++; break; }
}
}
}
for (int k = 0; k < 4; k++) { checkedA[k] = false; checkedB[k] = 0; }
result[i] = codeA[i] + "A" + codeB[i] + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] answer = new int[4];
while (scanner.hasNext()) {
for (int i = 0; i < 4; i++) answer[i] = scanner.nextInt();
int n = scanner.nextInt();
int[][] data = new int[n][4];
String[] result = new String[n];
for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) data[i][j] = scanner.nextInt();
result = nAnB(data, answer, n);
for (int i = 0; i < n; i++) System.out.println(result[i]);
}
}
}
試到一組測資:
3 4 0 0 1 1 0 0 2
應該是 1A1B
但你的 code 得到 1A0B
。
您的答案為: 1A0B 正確答案為: 1A1B
我測試過很多組測資都是正確的... 實在不懂這個測資到底是什麼#@#
有人也遇到一樣的嗎?@@
我的程式碼:
import java.util.Scanner;
public class Test {
static String[] nAnB(int[][] data, int[] answer, int n) {
String[] result = new String[n];
boolean[] checkedA = new boolean[4];
int[] checkedB = new int[4];
int[] codeA = new int[n], codeB = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
// check A
if (data[i][j] == answer[j]) {
if (checkedB[j] > 0) { codeB[i] -= checkedB[j]; checkedB[j] = 0; }
codeA[i]++; checkedA[j] = true; continue;
}
// check B
for (int k = 0; k < 4; k++) {
if (!checkedA[k] && checkedB[k] == 0) {
if (data[i][j] == answer[k]) { codeB[i]++; checkedB[k]++; break; }
}
}
}
for (int k = 0; k < 4; k++) { checkedA[k] = false; checkedB[k] = 0; }
result[i] = codeA[i] + "A" + codeB[i] + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] answer = new int[4];
while (scanner.hasNext()) {
for (int i = 0; i < 4; i++) answer[i] = scanner.nextInt();
int n = scanner.nextInt();
int[][] data = new int[n][4];
String[] result = new String[n];
for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) data[i][j] = scanner.nextInt();
result = nAnB(data, answer, n);
for (int i = 0; i < n; i++) System.out.println(result[i]);
}
}
}
其實我看不懂你的邏輯。
我是先比對完A的部分,再去比對B的部分。
A的部分比較簡單就不說了。
B的部分:已經是A的位置就不要比對了。(我是用boolean[]把位置存起來)
找到一個B也把這個B的位置存起來,下次比對時也跳過這個位置。(這裡我也是用一個boolean[]把位置存起來)
下面附上我的程式碼,希望你能先想過,不要直接拿來用。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.util.Scanner;
import java.util.Arrays;
public class a291 {
public static void main(String[] args) throws NumberFormatException, IOException {
// Scanner scan = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] ans = new int[4];
int n;
int[] guess = new int[4];
String str;
// while (scan.hasNext()) {
while ((str = br.readLine()) != null) {
// str = scan.nextLine();
String[] an = str.split(" ");
for (int i = 0; i < 4; ++i) {
// ans[i] = scan.nextInt();
ans[i] = Integer.parseInt(an[i]);
}
StringBuilder sb = new StringBuilder();
// n = Integer.parseInt(scan.nextLine());
n = Integer.parseInt(br.readLine());
// str = scan.nextLine();
for (int i = 0; i < n; ++i) {
// str = scan.nextLine();
str = br.readLine();
String[] s = str.split(" ");
guess[0] = Integer.parseInt(s[0]);
guess[1] = Integer.parseInt(s[1]);
guess[2] = Integer.parseInt(s[2]);
guess[3] = Integer.parseInt(s[3]);
// guess[0] = scan.nextInt();
// guess[1] = scan.nextInt();
// guess[2] = scan.nextInt();
// guess[3] = scan.nextInt();
boolean[] isCorrect = new boolean[4];
int a = 0, b = 0;
for (int j = 0; j < 4; ++j) {
if (ans[j] == guess[j]) {
a++;
isCorrect[j] = true;
}
}
boolean[] isCompare = Arrays.copyOf(isCorrect, isCorrect.length);
for (int j = 0; j < 4; ++j) {
if (!isCorrect[j]) {
for (int k = 0; k < 4; ++k) {
if (!isCompare[k] && ans[j] == guess[k] && j != k) {
b++;
isCompare[k] = true;
break;
}
}
}
}
sb.append(a + "A" + b + "B\n");
}
System.out.println(sb);
}
// scan.close();
}
}
您的答案為: 1A0B 正確答案為: 1A1B
我測試過很多組測資都是正確的... 實在不懂這個測資到底是什麼#@#
有人也遇到一樣的嗎?@@
我的程式碼:
import java.util.Scanner;
public class Test {
static String[] nAnB(int[][] data, int[] answer, int n) {
String[] result = new String[n];
boolean[] checkedA = new boolean[4];
int[] checkedB = new int[4];
int[] codeA = new int[n], codeB = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
// check A
if (data[i][j] == answer[j]) {
if (checkedB[j] > 0) { codeB[i] -= checkedB[j]; checkedB[j] = 0; }
codeA[i]++; checkedA[j] = true; continue;
}
// check B
for (int k = 0; k < 4; k++) {
if (!checkedA[k] && checkedB[k] == 0) {
if (data[i][j] == answer[k]) { codeB[i]++; checkedB[k]++; break; }
}
}
}
for (int k = 0; k < 4; k++) { checkedA[k] = false; checkedB[k] = 0; }
result[i] = codeA[i] + "A" + codeB[i] + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] answer = new int[4];
while (scanner.hasNext()) {
for (int i = 0; i < 4; i++) answer[i] = scanner.nextInt();
int n = scanner.nextInt();
int[][] data = new int[n][4];
String[] result = new String[n];
for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) data[i][j] = scanner.nextInt();
result = nAnB(data, answer, n);
for (int i = 0; i < n; i++) System.out.println(result[i]);
}
}
}
試到一組測資:
3 4 0 0 1 1 0 0 2
應該是 1A1B
但你的 code 得到 1A0B
。
我找到我的問題了~
感謝大大的測資讓我釐清概念^^
您的答案為: 1A0B 正確答案為: 1A1B
我測試過很多組測資都是正確的... 實在不懂這個測資到底是什麼#@#
有人也遇到一樣的嗎?@@
我的程式碼:
import java.util.Scanner;
public class Test {
static String[] nAnB(int[][] data, int[] answer, int n) {
String[] result = new String[n];
boolean[] checkedA = new boolean[4];
int[] checkedB = new int[4];
int[] codeA = new int[n], codeB = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
// check A
if (data[i][j] == answer[j]) {
if (checkedB[j] > 0) { codeB[i] -= checkedB[j]; checkedB[j] = 0; }
codeA[i]++; checkedA[j] = true; continue;
}
// check B
for (int k = 0; k < 4; k++) {
if (!checkedA[k] && checkedB[k] == 0) {
if (data[i][j] == answer[k]) { codeB[i]++; checkedB[k]++; break; }
}
}
}
for (int k = 0; k < 4; k++) { checkedA[k] = false; checkedB[k] = 0; }
result[i] = codeA[i] + "A" + codeB[i] + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] answer = new int[4];
while (scanner.hasNext()) {
for (int i = 0; i < 4; i++) answer[i] = scanner.nextInt();
int n = scanner.nextInt();
int[][] data = new int[n][4];
String[] result = new String[n];
for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) data[i][j] = scanner.nextInt();
result = nAnB(data, answer, n);
for (int i = 0; i < n; i++) System.out.println(result[i]);
}
}
}
其實我看不懂你的邏輯。
我是先比對完A的部分,再去比對B的部分。
A的部分比較簡單就不說了。
B的部分:已經是A的位置就不要比對了。(我是用boolean[]把位置存起來)
找到一個B也把這個B的位置存起來,下次比對時也跳過這個位置。(這裡我也是用一個boolean[]把位置存起來)
下面附上我的程式碼,希望你能先想過,不要直接拿來用。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.util.Scanner;
import java.util.Arrays;
public class a291 {
public static void main(String[] args) throws NumberFormatException, IOException {
// Scanner scan = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] ans = new int[4];
int n;
int[] guess = new int[4];
String str;
// while (scan.hasNext()) {
while ((str = br.readLine()) != null) {
// str = scan.nextLine();
String[] an = str.split(" ");
for (int i = 0; i < 4; ++i) {
// ans[i] = scan.nextInt();
ans[i] = Integer.parseInt(an[i]);
}
StringBuilder sb = new StringBuilder();
// n = Integer.parseInt(scan.nextLine());
n = Integer.parseInt(br.readLine());
// str = scan.nextLine();
for (int i = 0; i < n; ++i) {
// str = scan.nextLine();
str = br.readLine();
String[] s = str.split(" ");
guess[0] = Integer.parseInt(s[0]);
guess[1] = Integer.parseInt(s[1]);
guess[2] = Integer.parseInt(s[2]);
guess[3] = Integer.parseInt(s[3]);
// guess[0] = scan.nextInt();
// guess[1] = scan.nextInt();
// guess[2] = scan.nextInt();
// guess[3] = scan.nextInt();
boolean[] isCorrect = new boolean[4];
int a = 0, b = 0;
for (int j = 0; j < 4; ++j) {
if (ans[j] == guess[j]) {
a++;
isCorrect[j] = true;
}
}
boolean[] isCompare = Arrays.copyOf(isCorrect, isCorrect.length);
for (int j = 0; j < 4; ++j) {
if (!isCorrect[j]) {
for (int k = 0; k < 4; ++k) {
if (!isCompare[k] && ans[j] == guess[k] && j != k) {
b++;
isCompare[k] = true;
break;
}
}
}
}
sb.append(a + "A" + b + "B\n");
}
System.out.println(sb);
}
// scan.close();
}
}
您好 我的邏輯的確是怪怪的0.0
根據您的建議 重新思考後再寫了一遍成功AC
解題思路大致上是一樣的~ 感謝您喔
我的程式碼:
import java.util.Scanner;
public class JJavaJudgeTest {
static String[][] data;
static String[] nAnB(String[] password, int n) {
String[] result = new String[n];
boolean[] checkedA, checkedB;
int getA, getB;
for (int group = 0; group < n; group++) {
getA = 0; getB = 0; checkedA = null; checkedA = new boolean[4];
checkedB = null; checkedB = new boolean[4];
for (int i = 0; i < 4; i++) {
if (data[group][i].equals(password[i])) { getA++; checkedA[i] = checkedB[i] = true; }
}
if (getA < 4) {
for (int i = 0; i < 4; i++) {
if (checkedB[i]) continue;
for (int check = 0; check < 4; check++) {
if (!checkedA[check] && data[group][i].equals(password[check])) {
getB++; checkedA[check] = true; break;
}
}
}
}
result[group] = getA + "A" + getB + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String[] password = new String[4];
for (int i = 0; i < 4; i++) password[i] = scanner.next();
int n = scanner.nextInt();
data = new String[n][4];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) data[i][j] = scanner.next();
}
String[] result = nAnB(password, n);
for (int i = 0; i < n; i++)
System.out.println(result[i]);
}
}
}
您的答案為: 1A0B 正確答案為: 1A1B
我測試過很多組測資都是正確的... 實在不懂這個測資到底是什麼#@#
有人也遇到一樣的嗎?@@
我的程式碼:
import java.util.Scanner;
public class Test {
static String[] nAnB(int[][] data, int[] answer, int n) {
String[] result = new String[n];
boolean[] checkedA = new boolean[4];
int[] checkedB = new int[4];
int[] codeA = new int[n], codeB = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
// check A
if (data[i][j] == answer[j]) {
if (checkedB[j] > 0) { codeB[i] -= checkedB[j]; checkedB[j] = 0; }
codeA[i]++; checkedA[j] = true; continue;
}
// check B
for (int k = 0; k < 4; k++) {
if (!checkedA[k] && checkedB[k] == 0) {
if (data[i][j] == answer[k]) { codeB[i]++; checkedB[k]++; break; }
}
}
}
for (int k = 0; k < 4; k++) { checkedA[k] = false; checkedB[k] = 0; }
result[i] = codeA[i] + "A" + codeB[i] + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] answer = new int[4];
while (scanner.hasNext()) {
for (int i = 0; i < 4; i++) answer[i] = scanner.nextInt();
int n = scanner.nextInt();
int[][] data = new int[n][4];
String[] result = new String[n];
for (int i = 0; i < n; i++) for (int j = 0; j < 4; j++) data[i][j] = scanner.nextInt();
result = nAnB(data, answer, n);
for (int i = 0; i < n; i++) System.out.println(result[i]);
}
}
}
其實我看不懂你的邏輯。
我是先比對完A的部分,再去比對B的部分。
A的部分比較簡單就不說了。
B的部分:已經是A的位置就不要比對了。(我是用boolean[]把位置存起來)
找到一個B也把這個B的位置存起來,下次比對時也跳過這個位置。(這裡我也是用一個boolean[]把位置存起來)
下面附上我的程式碼,希望你能先想過,不要直接拿來用。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import java.util.Scanner;
import java.util.Arrays;
public class a291 {
public static void main(String[] args) throws NumberFormatException, IOException {
// Scanner scan = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] ans = new int[4];
int n;
int[] guess = new int[4];
String str;
// while (scan.hasNext()) {
while ((str = br.readLine()) != null) {
// str = scan.nextLine();
String[] an = str.split(" ");
for (int i = 0; i < 4; ++i) {
// ans[i] = scan.nextInt();
ans[i] = Integer.parseInt(an[i]);
}
StringBuilder sb = new StringBuilder();
// n = Integer.parseInt(scan.nextLine());
n = Integer.parseInt(br.readLine());
// str = scan.nextLine();
for (int i = 0; i < n; ++i) {
// str = scan.nextLine();
str = br.readLine();
String[] s = str.split(" ");
guess[0] = Integer.parseInt(s[0]);
guess[1] = Integer.parseInt(s[1]);
guess[2] = Integer.parseInt(s[2]);
guess[3] = Integer.parseInt(s[3]);
// guess[0] = scan.nextInt();
// guess[1] = scan.nextInt();
// guess[2] = scan.nextInt();
// guess[3] = scan.nextInt();
boolean[] isCorrect = new boolean[4];
int a = 0, b = 0;
for (int j = 0; j < 4; ++j) {
if (ans[j] == guess[j]) {
a++;
isCorrect[j] = true;
}
}
boolean[] isCompare = Arrays.copyOf(isCorrect, isCorrect.length);
for (int j = 0; j < 4; ++j) {
if (!isCorrect[j]) {
for (int k = 0; k < 4; ++k) {
if (!isCompare[k] && ans[j] == guess[k] && j != k) {
b++;
isCompare[k] = true;
break;
}
}
}
}
sb.append(a + "A" + b + "B\n");
}
System.out.println(sb);
}
// scan.close();
}
}
您好 我的邏輯的確是怪怪的0.0
根據您的建議 重新思考後再寫了一遍成功AC
解題思路大致上是一樣的~ 感謝您喔
我的程式碼:
import java.util.Scanner;
public class JJavaJudgeTest {
static String[][] data;
static String[] nAnB(String[] password, int n) {
String[] result = new String[n];
boolean[] checkedA, checkedB;
int getA, getB;
for (int group = 0; group < n; group++) {
getA = 0; getB = 0; checkedA = null; checkedA = new boolean[4];
checkedB = null; checkedB = new boolean[4];
for (int i = 0; i < 4; i++) {
if (data[group][i].equals(password[i])) { getA++; checkedA[i] = checkedB[i] = true; }
}
if (getA < 4) {
for (int i = 0; i < 4; i++) {
if (checkedB[i]) continue;
for (int check = 0; check < 4; check++) {
if (!checkedA[check] && data[group][i].equals(password[check])) {
getB++; checkedA[check] = true; break;
}
}
}
}
result[group] = getA + "A" + getB + "B";
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String[] password = new String[4];
for (int i = 0; i < 4; i++) password[i] = scanner.next();
int n = scanner.nextInt();
data = new String[n][4];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) data[i][j] = scanner.next();
}
String[] result = nAnB(password, n);
for (int i = 0; i < n; i++)
System.out.println(result[i]);
}
}
}
不用這麼客氣,我只是說出我的解法而已。
而且其實我也沒仔細看你的程式碼XD