spoj question BISHOPS. getting wrong answer. heres my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • depster
    New Member
    • May 2013
    • 7

    spoj question BISHOPS. getting wrong answer. heres my code

    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char a[109];
        int i,j,b[109],x,temp,t=1024;
        while(t>0)
        {
            scanf("%s",a);
            if(strlen(a)==1 && a[0]=='1')
            {
                printf("1");
            }
            else
            {
                if(a[strlen(a)-1]!='0')
                {
                    a[strlen(a)-1]=a[strlen(a)-1]-1;
                }
                else
                {
                    for(j=strlen(a)-1;a[j]=='0'&&j>0;j--)
                    {
                        a[j]='9';
                        if(a[j-1]!='0')
                        {
                            a[j-1]=a[j-1]-1;
                        }
                    }
                }
                j=0;
                for(i=strlen(a)-1;i>=0;i--)
                {
                    b[j]=a[i]-'0';
                    j++;
                }
                temp=0;
                for(i=0;i<j;i++)
                {
                    x=b[i]*2+temp;
                    b[i]=x%10;
                    temp=x/10;
                }
                while(temp>0)
                {
                    b[i]=temp%10;
                    temp=temp/10;
                    i++;
                }
                for(j=i-1;j>=0;j--)
                    printf("%d",b[j]);
            }
            printf("\n");
            t--;
        }
    
        return(0);
    
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You need to tell us what input you're using, what answer it's giving you, and what the answer is supposed to be.

    Comment

    • depster
      New Member
      • May 2013
      • 7

      #3

      here is the link to the problem. I'm getting all the answers correct on my compiler and even on ideone compiler but spoj compiler shows wrong answer

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I just put your code in ideone and the output is wrong. It outputs 2, 4, 2, and then a bunch of 1s. That does not match the expected output of 2, 4.

        Comment

        • depster
          New Member
          • May 2013
          • 7

          #5
          i have modified my code but still it is showing wrong answer on spoj compiler and correct answer on ideone
          earlier i was not able to detect eof but now i have corrected it. My algorithm just computes 2*(n-1) for large numbers and computes 1 for n=1. Is my algorithm correct?
          Code:
          #include<stdio.h>
          #include<string.h>
          int main()
          {
              char a[109];
              int i,j,b[109],x,temp;
              while(fscanf(stdin,"%s",a)==1)
              {
          
                  if(strlen(a)==1 && a[0]=='1')
                  {
                      printf("1");
                  }
                  else
                  {
                      if(a[strlen(a)-1]!='0')
                      {
                          a[strlen(a)-1]=a[strlen(a)-1]-1;
                      }
                      else
                      {
                          for(j=strlen(a)-1;a[j]=='0'&&j>0;j--)
                          {
                              a[j]='9';
                              if(a[j-1]!='0')
                              {
                                  a[j-1]=a[j-1]-1;
                              }
                          }
                      }
                      j=0;
                      for(i=strlen(a)-1;i>=0;i--)
                      {
                          b[j]=a[i]-'0';
                          j++;
                      }
                      temp=0;
                      for(i=0;i<j;i++)
                      {
                          x=b[i]*2+temp;
                          b[i]=x%10;
                          temp=x/10;
                      }
                      while(temp>0)
                      {
                          b[i]=temp%10;
                          temp=temp/10;
                          i++;
                      }
                      for(j=i-1;j>=0;j--)
                          printf("%d",b[j]);
                  }
                  printf("\n");
              }
              return(0);
          
          }

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            What answer is it showing on spoj?

            Comment

            • depster
              New Member
              • May 2013
              • 7

              #7
              it just shows wrong answer

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                It's hard to say then. If you don't know what the output is supposed to be, then that makes it difficult to know if your algorithm is correct. The only thing you can really do is study the problem to make sure your algorithm is correct.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Array a holds the digit characters of the grid size. Array b starts out holding something related to the digit numbers of the grid size and ends up holding the digit numbers of the result.

                  You mention 2*(n-1). The times-2 takes place on numbers at lines 37-49. The subtract-1 takes place on characters at lines 16-30. It would be a lot easier to also do the subtract-1 on numbers instead of characters. Borrow and carry are especially easier. Hint: 2*(n-1) = (2*n)-2.

                  Things to keep in mind:
                  • What if the result has more digits than the input? Where will you put the extra digit(s)?
                  • The number array won't be null-terminated.
                  • Your program will crash badly if the input string contains anything other than digits. I suppose you can trust spoj not to do that to you but ...
                  • Your program will crash badly if the input string is longer than array a. I suppose you can trust spoj not to do that to you but ...
                  • You have no comments! At the very least you should state your analytical solution to the Bishops problem.
                  Last edited by donbock; Jul 12 '13, 11:30 PM. Reason: Changed to reflect realization that array b holds numbers instead of characters.

                  Comment

                  Working...