Friday 15 January 2016

Solution of URI 1848 :: Counting Crow


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>
#include <string.h>
int main()
{
    int a,b,c,d,e,f,i,sum=0;
    char ara[10];
    for(i=1; i<=3; i++)
    {
        sum=0;
        while(1)
        {
            gets(ara);
            if(ara[0]=='c') break;

            if(ara[0]=='-')
            {
                if(ara[1]=='-')
                {
                    if(ara[2]=='-') sum+=0;
                    else sum+=1;
                }
                else
                {
                    if(ara[2]=='-') sum+=2;
                    else sum+=3;
                }
            }
            else if(ara[0]=='*')
            {
                if(ara[1]=='-')
                {
                    if(ara[2]=='-')sum+=4;
                    else sum+=5;
                }
                else
                {
                    if(ara[2]=='-') sum+=6;
                    else sum+=7;
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}

6 comments:

  1. why you are adding numbers 2,3,4,5,6,7 with the 'sum'?
    shoudn't it be just 1 or 0?

    ReplyDelete
    Replies
    1. Binary works with 1 being a different value depending on where it is on the number. Like: 001 means 1, 010 means 2, 100 means 4 and so on. The number to the left of the former is always its double.

      Delete
  2. i didnt use that much 'if', code below


    #include
    #include

    int main (){

    char entrada[10]; //used to get the inputs
    char pisca [10] = {"caw caw"}; // string used to compare when to sum
    int valores [3]; // values of the 3 numbers
    int soma = 0;
    int i;


    for (i=0; i <=2;i++) // // clear the values (just as a good coding practice)
    valores[i]=0;

    i = 0;


    do{
    gets (entrada);

    if(strcmp(pisca,entrada) == 0){ //
    valores [i] = soma; // sum the current numbers
    i++; // moves to the next number
    soma = 0; // clean the sum
    }

    if(entrada[0]== '*') // last bit = 2^2
    soma +=4;

    if(entrada[1]== '*') // second bit = 2^1
    soma +=2;

    if(entrada[2]== '*')// first bit = 2^0
    soma +=1;


    }while ( i<=2);

    for (i=0;i<=2;i++)
    printf("%d\n",valores[i]); // shows the 3 values





    return 0;
    }
    accepted

    ReplyDelete
  3. Here is My Code [Accepted]


    #include

    int main()
    {
    char s[50];
    int sum=0,i;
    for(i=0;i<3;i++){
    sum=0;
    while(1)
    {
    gets(s);
    if(s[0]=='*')sum+=4;
    if(s[1]=='*')sum+=2;
    if(s[2]=='*') sum+=1;

    if(s[0]>='c'&&s[6]=='w') break;
    }
    printf("%d\n",sum);
    }
    return 0;
    }

    ReplyDelete

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