HDOJ 猜數字(java)

猜數字

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2553    Accepted Submission(s): 1484


Problem Description
猜數字遊戲是gameboy最喜歡的遊戲之一。遊戲的規則是這樣的:計算機隨機產生一個四位數,然後玩家猜這個四位數是什麼。每猜一個數,計算機都會告訴玩家猜對幾個數字,其中有幾個數字在正確的位置上。
比如計算機隨機產生的數字爲1122。如果玩家猜1234,因爲1,2這兩個數字同時存在於這兩個數中,而且1在這兩個數中的位置是相同的,所以計算機會告訴玩家猜對了2個數字,其中一個在正確的位置。如果玩家猜1111,那麼計算機會告訴他猜對2個數字,有2個在正確的位置。
現在給你一段gameboy與計算機的對話過程,你的任務是根據這段對話確定這個四位數是什麼。
 

Input
輸入數據有多組。每組的第一行爲一個正整數N(1<=N<=100),表示在這段對話中共有N次問答。在接下來的N行中,每行三個整數A,B,C。gameboy猜這個四位數爲A,然後計算機回答猜對了B個數字,其中C個在正確的位置上。當N=0時,輸入數據結束。
 

Output
每組輸入數據對應一行輸出。如果根據這段對話能確定這個四位數,則輸出這個四位數,若不能,則輸出"Not sure"。
 

Sample Input
6 4815 2 1 5716 1 0 7842 1 0 4901 0 0 8585 3 3 8555 3 2 2 4815 0 0 2999 3 3 0
 

Sample Output
3585 Not sure
分析:此題的時間限制比較寬裕,參考了一下別人的思想,用的是蠻力解決,直接遍歷所有的四位數字,用每個四位數與題目中給出的數進行匹配,當所有的條件都滿足並且符合所有條件的數有且僅有一個才能唯一確定這個四位數,否則不能確定。
AC的java代碼如下:
import java.util.Scanner;

public class Main {
    
    static int[] a,b,c;

    public static void main(String[] args) {
        
        Scanner in  = new Scanner(System.in);
        while(in.hasNext()){
            
            int n = in.nextInt();
            if(n == 0)
                break;
            a = new int[n];
            b = new int[n];
            c = new int[n];
            
            for(int i=0; i<n; i++){
                a[i] = in.nextInt();
                b[i] = in.nextInt();
                c[i] = in.nextInt();    
            }
            
            int num = 0;
            int result = 0;
            for(int i=1000; i<=9999; i++){
                
                int j = 0;
                for(; j<n; j++){
                    
                    if(!guessBCount(i,a[j],b[j])){
                        break;
                    }
                    
                    if(!guessCCount(i,a[j],c[j])){
                        break;
                    }
                    
                    
                }
                if(j >= n){
                    num++;
                    result = i;
                }
                
                if(num >= 2 )
                    break;
                
                
            }
            
            if(num == 1){
                System.out.println(result);
            }else{
                System.out.println("Not sure");
            }
            
        }
        
        
        
    }

    private static boolean guessCCount(int a, int b, int k) {
        
        int temp1[] = new int[4];
        int temp2[] = new int[4];
        int count = 0;
        for(int m=0; m<4; m++){
            
            temp1[m] = a%10;
            a /= 10;
            temp2[m] = b%10;
            b /= 10;
            
            if(temp1[m] == temp2[m]){
                count++;
            }
            
        }
        
        if(count == k)
            return true;
        return false;
        
    }

    private static boolean guessBCount(int a, int b, int k) {
        
        int temp1[] = new int[4];
        int temp2[] = new int[4];
        int count = 0;
        for(int m=0; m<4; m++){
            
            temp1[m] = a%10;
            a /= 10;
            temp2[m] = b%10;
            b /= 10;
            
        }
        int[] visit = new int[4];
        for(int i=0; i<4; i++){
            
            for(int j=0; j<4; j++){
                
                if(visit[j] == 0 && temp1[i] == temp2[j]){
                    
                    count++;
                    visit[j] = 1;
                    break;
                }
                
                
            }

        }
        
        if(count == k)
            return true;
        return false;
    }

}


發佈了20 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章