자이의 프로그래밍
SWEA-1289 원재의 메모리 복구하기 본문
문제에서 요구하는대로 풀진 않았고 0->1로 바뀌는 횟수만큼? 예를 들어
0011이면 00->1로 바뀌는 순간 그 뒷부분에 대해 뒤집어야 하므로 그 부분을 else if 문에 넣었고
처음 수가 1인 경우에도 뒤집기? 를 진행해줘야 하기 때문에 문자열 끝까지 보면서 다음과 같이 풀었다.
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
char s[60];
int main() {
int tc;
cin >> tc;
for (int T = 1; T <= tc; T++) {
cin >> s;
int cnt = 0;
char before = '-1';
for (int i = 0; i<strlen(s); i++) {
if (cnt == 0 && s[i] == '1') {
cnt++;
before = s[i];
}
else if (cnt!=0&&s[i] != before) {
cnt++;
before = s[i];
}
}
cout << "#" << T << " " << cnt << '\n';
}
return 0;
}
'Algorithm > Cases-Study' 카테고리의 다른 글
카잉달력 (0) | 2021.05.04 |
---|---|
SWEA-1868 파핑파핑 지뢰찾기 (0) | 2021.04.28 |
SWEA-4466 최대 성적표 만들기 (0) | 2021.04.16 |
SWEA-2819 격자판의 숫자 이어붙이기 (0) | 2021.04.16 |
SWEA-5658 보물상자 비밀번호 (0) | 2021.04.16 |