๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1439๋ฒˆ - ๋’ค์ง‘๊ธฐ

by dkswnkk 2021. 11. 14.

๋ฌธ์ œ

 

1439๋ฒˆ: ๋’ค์ง‘๊ธฐ

๋‹ค์†œ์ด๋Š” 0๊ณผ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด S๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋‹ค์†œ์ด๋Š” ์ด ๋ฌธ์ž์—ด S์— ์žˆ๋Š” ๋ชจ๋“  ์ˆซ์ž๋ฅผ ์ „๋ถ€ ๊ฐ™๊ฒŒ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ๋‹ค์†œ์ด๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ํ–‰๋™์€ S์—์„œ ์—ฐ์†๋œ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์žก๊ณ  ๋ชจ

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    string s; cin >> s;
    int zero = 0, one = 0;
    int flag = -1;

    for (int i = 0; i < s.length(); i++) {     //์—ฐ์†๋œ ์ˆ˜ ๊ฐฏ์ˆ˜ ์ฒดํฌ
        if (s[i] == '0') {
            if (flag != 1) zero++;
            flag = 1;
        }
        else if (s[i] == '1') {
            if (flag != 0) one++;
            flag = 0;
        }
    }
    cout << min(zero, one);

}

๋Œ“๊ธ€