ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 숫자야구
    알고리즘 2023. 6. 15. 19:54
    728x90
    import java.util.*;
    
    public class TeamWork {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            // 서로 다른 3개의 숫자를 기억하는 것만들기
            Set<Integer> set = new HashSet<>(); // 중복을 허용 안함. 순서를 중요하게 생각 x
            while (true) {
                if(set.size() == 3){
                    break;
                }
                set.add((int) (Math.random() * 10));
            }
            List<Integer> list = new ArrayList<>(set);
    
            System.out.println("list = " + list);
    
    
            int ball;
            int strike = 0;
            int count = 0;
            String input;
            while (true) {
    
                // 종료 조건
                if(strike == 3) {
                    System.out.printf("%d번 만에 맞히셨습니다\n", count);
                    System.out.println("게임을 종료합니다.");
                    break;
                }
                strike = 0;
                ball = 0;
                count++;
    
                System.out.print(count + "번째 시도 : ");
                input = sc.next();
    
                String[] inputArr = input.split("");
    
                for (int i = 0; i < inputArr.length; i++) {
                    int num = Integer.parseInt(inputArr[i]);
                    if (list.contains(num)) {
                        if(list.get(i) == num) strike += 1;
                        else ball += 1;
                    }
                }
                System.out.println(ball + "B" + strike + "S");
    
            }
            sc.close();
        }
    }

    '알고리즘' 카테고리의 다른 글

    2798 - 블랙잭  (0) 2023.05.30
    11866 - 요세푸스 문제 0  (0) 2023.05.30
    11279 - 최대 힙  (0) 2023.05.30
    4949 - 균형잡힌 세상  (0) 2023.05.30
    9012 - 괄호  (0) 2023.05.26
Designed by Tistory.