cf1303a

CF1303

1. Erasing Zeroes

1.1 题意

​ 给你一个长度超过100的字符串,要求去掉最少的0,使得所有里面的1能够连续。

1.2 思路

​ 简单题,直接找到第一个1和最后一个1中间的0的数量,就是答案了。

1.3 代码
#include<bits/stdc++.h>
using namespace std; 
int const N=100+10; 
char s[N]; 
int n,t;  
int main(){
    scanf("%d",&t); 
    while (t--){
        scanf("%s",s+1);  
        int st=0,ed=0;  
        int len=strlen(s+1);  
        for(int i=1;i<=len;i++)  
            if(s[i]=='1') {
                st=i; 
                break;  
            }
        for(int i=len;i>=1;i--)  
            if(s[i]=='1'){
                ed=i;  
                break;  
            }
        if(!st && !ed){
            printf("%d\n",0);   
            continue;  
        }  
        int num=0; 
        for(int i=st;i<=ed;i++)  
            if(s[i]=='0') num++;  
        printf("%d\n",num);  
    }
    return 0; 
}
posted @ 2020-03-18 17:06  zjxxcn  阅读(71)  评论(0编辑  收藏  举报