Coding Discussions
Would you like to react to this message? Create an account in a few clicks or log in to continue.

ROWSOLD SOLUTION

2 posters

Go down

ROWSOLD SOLUTION Empty ROWSOLD SOLUTION

Post by Prateek Gupta Mon Apr 17, 2017 7:39 pm

Code:
#include <stdio.h>
 
int main()
{
    long long t,i,k,p1,p2,m,w,co,f;
    scanf("%lld",&t);
    char ch[1000005];
    for(i=1;i<=t;i++)
    {
        p1=0,p2=0,w=0,co=0,m=0,f=0;
        scanf("%s",ch);
        for(k=0;k<strlen(ch);k++)
        {
            if(ch[k]=='1')
            {
                p1=k;
                break;
            }
        }
        for(k=strlen(ch)-1;k>=0;k--)
        {
            if(ch[k]=='0')
            {
                p2=k;
                break;
            }
        }
        //printf("%d %d\n",p1,p2);
        m=0;
        for(k=p2;k>=p1;k--)
        {
            if(ch[k]=='0')
            {
                co++;
                if(f==0)
                {
                    m++;
                    f=1;
                }
            }
            else
            {
                w=w+co+m;
                f=0;
            }
        }
        printf("%lld\n",w);
    }
 return 0;
}

Prateek Gupta
Admin

Posts : 6
Join date : 2017-04-04

https://clanofcoders.board-directory.net

Back to top Go down

ROWSOLD SOLUTION Empty Re: ROWSOLD SOLUTION

Post by iamdsc_0410 Tue Apr 18, 2017 6:23 pm

#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t>0)
{
int time=0,c=0,c1,j=0,k,l;
char s[100001];
scanf("%s",s);
l=strlen(s);
for(k=l-1;k>=0;k--)
{
if(j==0)
{
if(s[k]=='0')
j=k;
}
if(s[k]=='1')
c++;
}
while(c>0)
{
c1=0;
for(l=0;l<j;l++)
{
if(s[l]=='1'&&s[l+1]=='0')
c1++;
}
time+=c1;
for(l=0;l<j;l++)
{
if(s[l]=='1'&&s[l+1]=='0')
{
s[l]='0';
s[l+1]='1';
time++;
}
}
j--;
c--;
}
printf("%d\n",time);
t--;
}
return 0;
}
iamdsc_0410
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 26
Location : IIIT Kalyani

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum