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 해야 된다는 것입니다.
'Algorithm > 백준+프로그래머스+SWEA+정올+구름' 카테고리의 다른 글
[Algorithm] SWEA 1873 상호의 배틀필드 (0) | 2021.08.04 |
---|---|
[Algorithm] SWEA 1208 Flatten (0) | 2021.08.04 |
[Algorithm] 백준 17478 재귀함수가 뭔가요? (0) | 2021.08.04 |
[Algorithm] 백준 1244 스위치 켜고 끄기 (0) | 2021.08.04 |
약수의 개수와 덧셈 (0) | 2021.07.01 |