Thursday, 14 January 2016

Solution of URI 1973 :: Star Trek


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    long long int N, i;
    scanf("%lld", &N);
    long long int star[N], sheep[N];
    long long int sum_trak=0, sum_sheep=0;
    for(i=0; i<N; i++)
    {
        scanf("%lld", &sheep[i]);
        star[i]=0;
    }
    i=0;
    while(1)
    {
        if(i==0 && sheep[i]%2==0)
        {
            star[i]=1;
            if(sheep[i]>0)
                sheep[i]--;
            break;
        }
        else if(i==N-1 && sheep[i]%2==1)
        {
            star[i]=1;
            if(sheep[i]>0)
                sheep[i]--;
            break;
        }
        else if(sheep[i]%2==1)
        {
            sheep[i]--;
            star[i]=1;
            i++;
        }
        else if(sheep[i]%2==0)
        {
            star[i]=1;
            if(sheep[i]>0)
                sheep[i]--;
            i--;
        }


    }
    for(i=0; i<N; i++)
    {
        sum_sheep += sheep[i];
        sum_trak += star[i];
    }
    printf("%lld %lld\n", sum_trak, sum_sheep);
    return 0;
}

5 comments:

  1. It was so helpfull...thanks a lot.may allah help you.

    ReplyDelete
  2. #include

    int main() {
    long long int n, i, sx=0, c=0;
    scanf("%lld", &n);
    long long int x[n], y[n];
    for( i=0; i 0) x[i]--;
    i += inc;
    } while( i >= 0 && i < n );

    for( i=0; i<n; i++ ) {
    sx += x[i];
    if ( x[i] != y[i] ) c++;
    }
    printf("%lld %lld\n", c, sx);
    return 0;
    }

    ReplyDelete
  3. Can you explain that?? please.....

    ReplyDelete
  4. The program run correctly but in uri it showing 'runtime error'.
    I cant find where the problem is.

    ReplyDelete

Note: only a member of this blog may post a comment.