A. Erasing Zeroes from Educational Codeforces Round 82 (Rated for Div. 2)
程序员文章站
2022-06-02 11:30:36
...
题目链接:https://codeforces.com/contest/1303/problem/A
题意:一个0和1构成的字符串,让所有的1都连续在一起,问你要消除多少个0?
思路:找到所有的零,记录第一个1和最后一个1的位置,再对中间的零进行计算,如果都是0直接输出0。
#include<stdio.h>
#include<string.h>
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
char a[10005];
int k=0;
scanf("%s",a);
int i,x=0,y=0,m=0,x1=0;
for(i=0; i<strlen(a); i++)
{
if(a[i]=='0')
{
m++;
}
if(a[i]=='0'&&x1==0)
{
x++;
}
if(a[i]=='1')
{
x1++;
}
}
for(i=strlen(a)-1; i>=0; i--)
{
if(a[i]=='0')
{
y++;
}
else
break;
}
if(x1==0)
{
printf("0\n");
}
else
{
printf("%d\n",m-x-y);
}
}
return 0;
}
推荐阅读
-
Educational Codeforces Round 93 (Rated for Div. 2) A. Bad Triangle
-
A. Erasing Zeroes from Educational Codeforces Round 82 (Rated for Div. 2)
-
Educational Codeforces Round 52 (Rated for Div. 2) A. Vasya and Chocolate
-
Educational Codeforces Round 51 (Rated for Div. 2).A. Vasya And Password(模拟)
-
Educational Codeforces Round 93 (Rated for Div. 2) A. Bad Triangle