I♥TLE

Java、オンラインジャッジなど

0025-Hit and Blow

問題(外部リンク)

Hit and Blow | Aizu Online Judge

実装の概要

AとBの数列を比較し、数字がちょうど一致しているか、あるいは一致していなくても数列に含まれているかを判断する問題です。
今回の実装では入力を受け付ける際に「その数がAの数列に含まれているか」についても保存しています。これによりブローの判定を行いやすくなります。

public class Main {
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        while(true){
            String tmpStr = br.readLine();
 
            if(tmpStr == null){
                break;
            }
 
            int[] a = new int[4];
            int[] b = new int[4];
            //Aの中にその数字が含まれているか
            boolean[] inA = new boolean[10];
            Arrays.fill(inA, false);
 
            String[] tmpArray1 = tmpStr.split(" ");
            String[] tmpArray2 = br.readLine().split(" ");
 
            for(int i = 0; i < 4 ; i++){
                a[i] = Integer.parseInt(tmpArray1[i]);
                b[i] = Integer.parseInt(tmpArray2[i]);
                inA[a[i]] = true;
            }
            
            int hit = 0;
            int blow = 0;
            for(int i = 0; i < 4; i++){
                if(a[i] == b[i]){
                    hit++;
                }
                 
                else if(inA[b[i]]){
                    blow++;
                }
            }
             
            System.out.println(hit + " "+ blow);
        }
    }
 
}