[SWEA] 1289. 원재의 메모리 복구하기 D3 - Array

제출일 : 2019-07-16

문제 풀이 시간 : 15M

난이도 : ★☆

Problem

link : https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV19AcoKI9sCFAZN

Input

첫 번째 줄에 테스트 케이스의 수 T가 주어진다.

각 테스트 케이스는 한 줄로 이루어져 있으며, 메모리의 원래 값이 주어진다.

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

Output

각 테스트 케이스마다 ‘#x’(x는 테스트케이스 번호를 의미하며 1부터 시작한다)를 출력하고,

초기값(모든bit가 0)에서 원래 값으로 복구하기 위한 최소 수정 횟수를 출력한다.

Example

input

2
0011
100

output

#1 1
#2 2

Solution & Inpression

문제에서 초기값은 모든 비트가 0이라고 정해져있다.

오른쪽부터 현재 메모리의 값(해당 비트의 값)이 복구하고자 하는 메모리 값이 아니라면 바꾸고자 하는 값으로 현재비트부터 첫번째 비트까지 값을 바꾸어 준다.

위작업을 비트수만큼 진행한다.

Code

언어 : JAVA

메모리 : 20,540 kb

실행시간 : 138 ms

import java.util.Arrays;
import java.util.Scanner;
import java.io.FileInputStream;
import java.lang.reflect.Array;

public class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();

        for(int tc = 1; tc <= T; tc++){

            char[] input = sc.next().toCharArray(); 
            char[] tmp = new char[input.length()];

            int cnt=0;

            for(int i =0 ; i<input.length();i++) {
                tmp[i]='0';
            }


            for(int i =0 ; i<input.length();i++) {
                if(input[i]!=tmp[i]) {
                    char tmpNum=input[i];
                    for(int j=i; j<input.length();j++) {
                        tmp[j]=tmpNum;
                    }
                    cnt++;
                }       
            }
            System.out.println("#" + tc + " " + cnt);
        }
    }
}