[SWEA] 원재의 메모리 복구하기

문제 보러가기

제한사항

메모리의 길이는 1이상 50이하이다.

첫번째 생각

앞자리부터 하나하나 고쳐가며 답을 확인한다.

Code

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
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		sc.nextLine();
		for(int tc=1;tc<=T;tc++) {
			String str = sc.nextLine();
			int len = str.length();
			String[] strN = str.split("");
			int[] N = new int[len];
			for(int i=0; i<len; i++) {
				N[i] = Integer.parseInt(strN[i]);
			}
			
			int count = 0;
			int[] answer = new int[len];
			for(int i=0; i<len; i++) {
				if ( answer[i] == N[i] ) {
					continue;
				} else if ( answer[i] != N[i] ) {
					if ( answer[i] == 0 ) {
						for(int j=i; j<len; j++) {
							answer[j] = 1;
						}
					} else {
						for(int j=i; j<len; j++) {
							answer[j] = 0;
						}
					}
					count++;
				}
			}
			System.out.println("#"+tc+" "+count);
		}
	}
}