HihoCoder 1326 有序01字符串 暴力枚举

题目链接


暴力枚举结果情况,然后跟当前比较,找到最小次数

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int t;
char s[1005];
int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", s);
        int n = strlen(s);
        int ans = 1e9;
        for(int i = 0; i <= n; i++)
        {
            int tmp = 0;
            for(int j = 0; j < i; j++)
                if(s[j] != '0') tmp++;
            for(int j = i; j < n; j++)
                if(s[j] != '1') tmp++;
            ans = min(tmp, ans);
        }
        printf("%d
", ans);
    }
    return 0;
}
如果有错误,请指出,谢谢
原文地址:https://www.cnblogs.com/Alruddy/p/7398417.html