Q340: Master-Mind Hints
Master-Mind是一種2個人的遊戲。其實就是學生常玩的幾A幾B的遊戲(規則或許有些許不同)。其中一個人擔任出題者,他選一串1到9數字當作密碼(可以重複)。另一個人為解題者,他要去猜密碼為何。解題者每次猜測後,出題者以某種格式回答他這次猜的結果。在遊戲開始之前,雙方先協議密碼的長度,假設是n。在出題者設定密碼(s1,s2,...sn)後,由解題者來猜(g1,g2,...gn)。如果同樣位置gi=si,那解題者得到一個A。如果gi=sj,但i不等於j,那解題者得到一個B。請注意:不管是得A或得B,每個gi最多只能對應到一個sj,而且得A比得B優先。舉例說明:密碼為1 2 3 4,若猜題者猜1 1 5 6,那出題者應該回答1A0B,而不是0A1B。
如果某個猜測得到 nA0B,那就代表猜題者完全猜中密碼了。
Input
輸入含有多組測試資料。每組測試資料的第一列含有1個正整數 N(N <= 1000),代表密碼的長度。第二列有N個1到9的數字,代表密碼。接下來有數量不等的猜測,每個一列,亦含有N個1到9的數字。若此猜測的N個數字均為0,代表此組測試資料結束。N=0代表整個輸入結束。請參考Sample Input。
Output
對每一組測試資料,輸出這是第幾組。然後輸出出題者回答猜題者各個猜測的結果是幾A幾B,以(A,B)的形式表示。請參考Sample Output。
Sample Input
4 1 3 5 5 1 1 2 3 4 3 3 5 6 5 5 1 6 1 3 5 1 3 5 5 0 0 0 0 10 1 2 2 2 4 5 6 6 6 9 1 2 3 4 5 6 7 8 9 1 1 1 2 2 3 3 4 4 5 5 1 2 1 3 1 5 1 6 1 9 1 2 2 5 5 5 6 6 6 7 0 0 0 0 0 0 0 0 0 0 0
Sample Output
Game 1: (1,1) (2,0) (1,2) (1,2) (4,0) Game 2: (2,4) (3,2) (5,0) (7,0)
http://luckycat.kshs.kh.edu.tw/homework/q340.htm
import java.util.Scanner;
public class UVA_340 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n;
int game=1;
while((n=sc.nextInt())!=0){
int[] pass1=new int[n];
for(int i=0;i<n;i++)
pass1[i]=sc.nextInt();
int[] pass=new int[n];
int[] guess=new int[n];
System.out.println("Game "+game++ +":");
while(true){
for(int i=0;i<n;i++){
guess[i]=sc.nextInt();
pass[i]=pass1[i];
}
if(guess[0]==0) break;
int A=0,B=0;
for(int i=0;i<n;i++){
if(pass[i]==guess[i]){
A++;
guess[i]=0;
pass[i]=0;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(guess[i]==0)
continue;
else{
if(guess[i]==pass[j]){
B++;
guess[i]=0;
pass[j]=0;
}
}
}
}
System.out.println("("+A+","+B+")");
}
}
}
}
沒有留言:
張貼留言