본문으로 바로가기

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

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 


 

import java.util.Scanner;

public class Solution {
	
	public static void main (String[] args) {
	         
		Scanner sc = new Scanner(System.in);
	         
	    int T = sc.nextInt();
	    for (int t = 0; t < T; t++) {
	        String origin = sc.next();
	         
	        char[] bit = origin.toCharArray();
	      
	        int count = origin.charAt(0) == '0' ? 0 : 1;
	             
	        for (int i = 0; i < bit.length - 1; i++) {
	        	if (bit[i] != bit[i+1])
	                count++;
	        }
	             
	        System.out.println("#" + (t+1) + " " + count);
	    }
	}
}

 

0에서 1로 바꾸는 횟수(즉, "01"의 개수)와 1에서 0으로 바꾸는 횟수(즉, "10")의 횟수를 출력하면 된다고 생각했습니다.

유의할 점은 처음에 초기화 값이 0으로만 이루어져 있기 때문에, 처음에 1로 시작한다면 +1 해야 된다는 것입니다.