ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [알고리즘 문제] 코딩도장 로또프로그램
    IT/알고리즘 2019. 5. 5. 14:42


    코딩도장에서 leak이라는 분이 내주신 문제!.! 

    로또를 사본적이 별로 없어서 실제 어떤 방식인지 몰라 문제와 똑같이 구현했다.



    소스코드 ↓↓↓


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    public class lottoProgram {
     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            Scanner scan = new Scanner(System.in);
            
            int[] lotto = new int[7];    //로또 당첨 번호
            
            //당첨번호 랜덤으로 받기
            for(int i=0; i<7; i++){
                lotto[i] = (int)(Math.random()*45)+1;
                for(int j=0; j<i; j++){
                    if(lotto[i] == lotto[j]){
                        i--;
                        break;
                    }
                }
            }
            
            //구매할 로또 개수
            System.out.print("로또를 몇개 구매하시겠습니까? : ");
            int number = scan.nextInt();
            
            
            //당첨번호 출력
            System.out.print("현재 당첨번호는 ");
            for(int i=0; i<6; i++){
                System.out.print(lotto[i] + ",");
            }
            System.out.println(" 보너스 번호는 " + lotto[6+ "입니다.");
            
            System.out.println();
            
            int[] mylotto = new int[7];    //구매한 로또번호
            
            for(int k=0; k<number; k++){
                
                int count = 0;    //일치하는 번호 개수
                
                //구매한 랜덤번호 
                for(int w=0; w<7; w++){
                    mylotto[w] = (int)(Math.random()*45)+1;
                    for(int p=0; p<w; p++){
                        if(mylotto[w] == mylotto[p]){
                            w--;
                            break;
                        }
                    }
                }
                
                
                System.out.print("구매하신 추첨번호는 ");
                for(int z=0; z<6; z++){
                    System.out.print(mylotto[z] + ",");
                }
                System.out.print(" 보너스 번호는 " + mylotto[6]);
                
                
                //당첨번호와 일치하는 숫자가 있으면 count 증가
                for(int i=0; i<7; i++){
                    for(int j=0; j<7; j++){
                        if(lotto[i] == mylotto[j]){
                            count++;
                        }
                    }
                }
     
                switch(count){
                    case 7:
                        System.out.println(".. 1등이다!! 아싸 오늘 저녁은 치킨이닭!!");
                        break;
                    case 6:
                        System.out.println(".. 2등이다!!" + "일치하는 번호는 " + count + "개 입니다.");
                        break;
                    case 5:
                        System.out.println(".. 3등이다!!" + "일치하는 번호는 " + count + "개 입니다.");
                        break;
                    case 4:
                        System.out.println(".. 4등이다!!" + "일치하는 번호는 " + count + "개 입니다.");
                        break;
                    case 3:
                        System.out.println(".. 5등이다!!" + "일치하는 번호는 " + count + "개 입니다.");
                        break;
                    default :
                        System.out.println("입니다. " + "일치하는 번호는 " + count + "개 입니다.");
                        break;
                }
                
                //1등 당첨되면 count 종료
                if(count == 7){
                    break;
                }
                 
            }
                
        }
     
    }
    cs



    실행결과↓


    10개중에 5등 당첨 한개! 실제 복권에도 5등이 있나...?




    1000개 돌렸는데 겨우 4등 당첨됬다..! 이 전에 만개도 돌리고 해봤는데 최대 3등까지 밖에 못봄 ㅠㅠㅠㅠ 치킨은 볼 수 없었다고 한다....


    댓글

Designed by Tistory.