XidianOJ 1154 Nhywieza 的串

题目描述

输入

输出

对于每组数据输出 1 行,表示最少的操作次数。

--正文

找到连续的0一起变就好

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

char str[10001];

void Solve(char* str){
    int len = strlen(str);
    int flag = 0,now = 0,before = -1;
    int len0 = 0;
    while (now < len) {
        if (str[now] == '1'){
            if (!flag) {
                now ++;
                continue;
            }
            if (before == 0){
                before = 1; len0 ++;
            }
            before = 1;
        }
        if (str[now] == '0'){
            flag = 1;
            if (before == 1){
                before = 0;
            }
            before = 0;
        }
        now ++;
    }
    if (str[len-1] == '0'){
        len0 ++;
    }
    
    printf("%d
",len0);
}

int main(){
    while (scanf("%s",str) != EOF){
        Solve(str);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ToTOrz/p/6179593.html