알고리즘 기초 - 20 ( 3.6.9 게임 )

Algorithm/풀었던문제 2019. 12. 20. 15:44

 

 

 

 

Main.java

 

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
import java.io.IOException;
import java.util.Scanner;
 
public class Main {
 
    //메인 메소드가 static이므로 static로 3.6.9가 
    //들어갔을때 숫자를 반환하는 메소드를 만든다.
 
    public static int getNum (int i){
        
        //3,6,9가 들어갔을때 카운트 해주는 변수 
        int j = 0;
        
        while(i > 0){
            
            //숫자를 한자리씩 계산하기 위해 10으로 나누어서 나머지가 3,6,9와 같으면
            //카운트 변수를 1씩 증가시키고, 다음자리수를 
            //계산하기 위해 i를 다시 10으로 나누어준다.
            
            if( i % 10 == 3 || i % 10 == 6 || i % 10 == 9)
            {
                j++;
            i /= 10;
                
            }
            
        }
        
        return j;
        
    }
 
 
    public static void main(String[] str) throws IOException {
        
    //100까지 3.6.9 게임 진행
    
    int number = 100;
    
    for(int i = 1; i <= number; i++){
        
        
        //위에 있는 함수로 계산한 값을 cnt 변수에 저장
       int cnt = getNum(i); 
       
       //cnt가 0이면 3,6,9 중에 숫자가 안들어있다는 것이기 때문에
       //숫자만 출력
       
       if (cnt == 0){
           
           System.out.print(i+" ");
           
       }else {
           
           for (int l = 0; l < cnt; l++){
               
               System.out.print("짝");
           }
           
           System.out.print(" ");
           
       }
       
    }
    
}
 
 
cs

 

 

 

출력 결과

 

 

 

출처

 

https://www.youtube.com/watch?v=rXNm4YpPVNc&list=PLVoihNyHW4xkm_KJ8_N8X7F6EQP4uSRyR&index=21

 

: