problem link:
Discuss: in this problem you can use for loop for count 0 and a variable to count the position value of 0.if there is sequence of 0's then increase the value of position variable and add it with sum.if it is not 0 then make the value of position variable 1.
try yourself before see the code
#include<stdio.h>
int main()
{
int i,j,t,n,sum,k;
char str[100];
scanf("%d",&t);
for(i=1;i<=t;i++)
{
sum=0;
scanf("%s",str);
n=strlen(str);
k=1;
for(j=0;j<n;j++)
{
if(str[j]=='O')
{
sum+=k;
k++;
}
else if(str[j]=='X')
k=1;
}
printf("%d\n",sum);
}
return 0;
}
Discuss: in this problem you can use for loop for count 0 and a variable to count the position value of 0.if there is sequence of 0's then increase the value of position variable and add it with sum.if it is not 0 then make the value of position variable 1.
try yourself before see the code
#include<stdio.h>
int main()
{
int i,j,t,n,sum,k;
char str[100];
scanf("%d",&t);
for(i=1;i<=t;i++)
{
sum=0;
scanf("%s",str);
n=strlen(str);
k=1;
for(j=0;j<n;j++)
{
if(str[j]=='O')
{
sum+=k;
k++;
}
else if(str[j]=='X')
k=1;
}
printf("%d\n",sum);
}
return 0;
}
No comments:
Post a Comment