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

Time Lapse :NONE

1289.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main() {
int tc = 1, T;
char c, prev;
scanf("%d",&T);
getchar_unlocked();
while(T--) {
prev = c = getchar_unlocked();
int ans = 1;
bool flag = c == '0' ? false : true;
for(c = getchar_unlocked(); c == '1' || c == '0' ; prev = c, c = getchar_unlocked()) {
if(flag && prev != c) ++ans;
if(c == '1') flag = true;
}
printf("#%d %d\n",tc++, ans);
}
}

1289.java

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

public class Solution {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int tc = 1;
int T;
String input;
T = sc.nextInt();
while(T-- != 0) {
input = sc.next();
int ans = 1;
boolean flag = false;
for(int i = 0; i < input.length() - 1; ++i) {
if(input.charAt(i) == '1')
flag = true;
if (flag) {
if(input.charAt(i) != input.charAt(i + 1)) ++ans;
}
}
System.out.println("#" + tc++ + " " + ans);
}

}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/1289/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.