알고리즘
[Java] SWEA - 원재의 메모리 복구하기
J3SUNG
2023. 1. 11. 22:47
728x90
문자의 길이가 n일때 2번째 비트를 1로 변경하면 2~n번이 전부 1로 변경됩니다.
마찬가지로 0으로 변경하면 0으로 전부 변경됩니다.
이를 통해 0인 값을 몇번반에 입력된 값으로 변경할 수 있는지 찾는 문제입니다.
왼쪽부터 처음 1이 나오는 위치를 찾고
그 후로 이전 값이랑 값이 다를때마다 카운트를 해주어서 출력해주었습니다.
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T;
int result = 0;
char c;
String s;
T=sc.nextInt();
s=sc.nextLine();
for(int test_case = 1; test_case <= T; test_case++)
{
c = '0';
result = 0;
s=sc.nextLine();
for(int i=0; i<s.length(); ++i) {
if(c == s.charAt(i)) {
continue;
}
else {
c = s.charAt(i);
++result;
}
}
System.out.println("#" + test_case + " " + result);
}
}
}