這字幕時間實在抓的真好.....
------------------------------------------------------------------------------------------------------------------------------
猜數字,一般都是電腦出題讓我們猜,然後告訴我們幾A幾B作提示
不過我是挑戰逆向操作
我們告訴電腦幾A幾B,讓電腦猜出我們所預設的答案
目前只能用最基礎的寫法
不過也可以在10次內猜出來了.....(應該)
更詳細的寫法,等我想到在說(爆)
-------------------------------------------------------------------------------------------------------------
import java.io.*;
public class guessnum {
String[] ansary = new String[5040];
public guessnum(){
int st=0;
System.out.println("說明:AB部分請輸入一二位數。");
System.out.println("例:1A2B時請輸入12,0A3B時請輸入03。");
for (int n1=0; n1<10; n1++) {
for (int n2=0; n2<10; n2++) {
if ( n2 == n1)
continue;
for (int n3=0; n3<10; n3++) {
if (n3 == n2 || n3 == n1 )
continue;
for (int n4=0; n4<10; n4++) {
if ( n4==n1 || n4==n2 || n4==n3)
continue;
ansary[st] = Integer.toString(n1)+n2+n3+n4;
//System.out.println(ansary[st]);
st++;
}}}}
plga();
}
public void plga(){
/* boolean b = true;
int i = Math.random()*10000-1;
while
if(i<5040)
*/
for (int i=0; i<5040; i++) {
if (ansary[i].equals("-9")) continue;
enter(ansary[i]);
}
System.out.println("本題答案不符規定!");
}
public void enter(String str){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int w=0,d=0;
try{
System.out.println(str+"? 請輸入一二位數:");
System.out.print("→");
String input = br.readLine();
while ( input.length() != 2 ) {
System.out.println("輸入錯誤,請重新輸入:");
System.out.println(str+"? 請輸入一二位數:");
System.out.print("→");
input = br.readLine();
}
w = input.charAt(0) - '0';
d = input.charAt(1) - '0';
}
catch(IOException e){
System.out.println("本題答案不符規定!");
System.exit(0);
}
if (w == 4) {
System.out.println("答案為:" +str);
System.exit(0);
}
deleteans(str,w,d);
}
public void deleteans(String s,int a,int b){
for(int x=0;x<5040;x++){
if(ansary[x].equals("-9"))
continue;
if(Ast(ansary[x],s)!=a || Bnd(ansary[x],s)!=b)
ansary[x] = "-9";
}
}
public static int Ast(String s1,String s2){
int a = 0;
for(int i=0;i<4;i++){
if(s1.charAt(i) == s2.charAt(i))
a++;
}
return a;
}
public static int Bnd(String s1,String s2){
int b = 0;
//System.out.println(s1);
//System.out.println(s2);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(i==j)continue;
if(s1.charAt(i) == s2.charAt(j))
b++;
}
}
return b;
}
public static void main(String[] alio){
guessnum pl = new guessnum();
}
}
留言列表