카테고리 없음

SWEA) 1289 원재의 메모리 복구하기

이불이! 2019. 5. 8. 17:36
728x90

첫번째 코드

에러사항 1 ) 처음에 0이 들어와도 카운트, 다음 숫자가 전에 숫자랑 같아도 카운트가 되기 때문에 실패

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
package Solution_1289;
 
import java.util.Scanner ;
 
public class main_1289 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        int T = sc.nextInt();
        int cnt = 0;
        
        for(int i=1; i<=T; i++) {
            int num = sc.nextInt() ;
            int [] array = new int[num] ;
            int [] copy = new int[num] ;
            
            for(int j=0; j<array.length; j++) {
                if(array[j] == 1) {
                    for(j=0; j<array.length; j++) {
                        copy[j] = 1;
                        cnt++;
                    }
                } else {
                    for(j=0; j<array.length; j++) {
                        copy[j] = 0;
                        cnt++ ;
                    }
                }
                    
            }
        }
        for(int i=1; i<=T; i++) {
            System.out.printf("#%d %d \n", i, cnt);            
        }
    }
    
}
 
 

 

두번째 코드

일단 정수 -> 배열로 변경하는데서 에러가 발생하는데 ,, 이건 배열의 요솟수를 구하는 거라 탈락..

에러 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Solution_1289.main_1289.main(main_1289.java:23)

* 자바 기초가 딸리니까 정수를 어떻게 배열로 변환해야하는지, 아니면 정수를 배열로 바꾸는게 맞는지부터 모르겠다 ㅜ

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
package Solution_1289;
 
import java.util.Scanner ;
 
public class main_1289 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        int T = sc.nextInt();
        int cnt = 0;
        
        for(int i=1; i<=T; i++) {
            int num = sc.nextInt();
            int[] array = new int[num] ;
            int [] copy = {0,0,0,0} ;
            
            System.out.println(array[1]) ;
            
            for(int j=0; j<array.length; j++) {
                if(array[j] != copy[j]) {
                    cnt ++ ;
                    for(j=0; j<array.length; j++) {
                        copy[j] = 1;
                    }
                }
            }
        }
        for(int i=1; i<=T; i++) {
            System.out.printf("#%d %d \n", i, cnt);            
        }
    }
    
}
 
 
 

 

세번째 코드

방법을 찾다가 String으로 입력받은 후 char 배열로 변경해주었다! 하지만 배열에 직접 0인 수를 넣어준다면

다른 수가 들어왔을 때 에러발생

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
package Solution_1289;
 
import java.util.Scanner ;
 
public class main_1289 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        int T = sc.nextInt();
        int cnt = 0;
        
        for(int i=1; i<=T; i++) {
            String num = sc.next();
            char [] array = num.toCharArray() ;
            char [] copy = {0,0,0,0} ;
            
            
            for(int j=0; j<array.length; j++) {
                if(array[j] != copy[j]) {
                    cnt ++ ;
                    for(j=0; j<array.length; j++) {
                        copy[j] = 1;
                    }
                }
            }
        }
        for(int i=1; i<=T; i++) {
            System.out.printf("#%d %d \n", i, cnt);            
        }
    }
    
}
 
 
 

 

네번재 코드

코드가 너무 길어지는 것 같긴한데 ;

일단 char [] num 을 한거번에 배열로 받아주었고, copy[]를 num의 배열의 길이로 '0'으로 변경해주었다.

하나만 입력하면 잘 입력되는데 두개이상 입력하면 에러발생한다ㅜㅜ 왜그러지

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
package Solution_1289;
 
import java.util.Scanner ;
 
public class main_1289 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        int T = sc.nextInt();
        int cnt = 0;
        
        for(int i=1; i<=T; i++) {
            char [] num = sc.next().toCharArray();
            char [] copy = new char[num.length] ;
            
            for(int j=0; j<num.length; j++) { 
                copy[j] = '0' ; 
            } 
            
            for(int j=0; j<num.length; j++) {
                if(num[j] != copy[j]) {
                    for(int k=j; k<num.length; k++) {
                        if(num[j] == '1') {
                            copy[k] = '1';
                        } else {
                            copy[k] = '0' ;
                        }
                    }
                    ++cnt ;
                }
 
            }
        }
        for(int i=1; i<=T; i++) {
            System.out.printf("#%d %d \n", i, cnt);            
        }
    }
    
}
 
s

 

다섯번째 코드

자꾸 두번째 cnt에서 에러가 발생했는데 초기화를 잘못해서 그랬음 크큼,, 완성~

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
package Solution_1289;
 
import java.util.Scanner ;
 
public class main_1289 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        int T = sc.nextInt();
        
        for(int i=1; i<=T; i++) {
            char [] num = sc.next().toCharArray();
            char [] copy = new char[num.length] ;
            int cnt = 0;
            
            for(int j=0; j<num.length; j++) { copy[j] = '0' ; } 
            
            for(int j=0; j<num.length; j++) {
                if(num[j] != copy[j]) {
                    for(int k=j; k<num.length; k++) {
                        if(num[j] == '1') {
                            copy[k] = '1';
                        } else {
                            copy[k] = '0' ;
                        }
                    }
                    cnt ++ ;
                }
            }
            System.out.printf("#%d %d \n", i, cnt);
        }
    }
}
 
 
 

 

성우오빠가 문제열심히 알려줬는데,, 생각보다 직장다니면서 병행하기가 쉽지 않았다..

미안해..ㅁ7ㅁ8