Thread: Help needed with SPOJ solution.

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    8

    Help needed with SPOJ solution.

    Hello, i have been experiencing issues with a simple problem from SPOJ website. Sphere Online Judge (SPOJ) - Problem NDIGITS

    This is the code im working on.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void contador(short t[], int n)
    {
        int i;
        
         if(n == 1)
    
           t[0] = 10;
    
         else
         { 
           t[0] = 9;
               
           for (i = 0; i < n ;i++)
              t[i+1] = 0;
         }     
    }
    
    
    
    
    int main()
    {
        int n , i , d , m;
        short t[999];
        
        scanf(" %d ",&n);
        
        for(i = 0;i < n ;i++)
        {
          scanf(" %d ",&d);
          
            contador(t, d);
            
            if(n == 0)
             printf(" %d ", 0);
            else
              for (m = 0; m < d; m++)
               printf(" %d ", t[m]);
               
               
               printf(" \n ");
          
          
            
        }
        
       
     return 0;   
    }



    The solution itself isnt a big deal but given the amount of digit to look for it can become an issue.

    Basically i made an array of X amount of space (in this case 1000) and i filled it with a y amount of spaces with 0s, except the first space array[0] which will remain unchanged in (almost) all the cases.

    There is no way to save numbers with 50,60 or even a lot more in a variable so the only thing i could do was make an array and simply save every digit in a space and just print it out one by one. The problem is, according to the judge, the answer is wrong. I donīt know why, i want to, i keep changing it over and over again and now instead of programming im just trying to guess.

    Hope you guys can help me out here and thanks in advance.
    Last edited by mdrgon; 07-14-2013 at 08:57 PM.

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Fix the indentation in your code, and better explain what you think your problem is please. The Judge said it was wrong is not very descriptive to me.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    8
    I did my best but im still not experienced enough nor worked enough to get it right.

    About what i think the problem is... Im really not sure. It might be the way i made the output. I dont beleive it should matter but i really dont know. Like i said the output is one by one instead of just a ĻfullĻ number. Like you said, the judge saying it is wrong doesnt help me very much neither. There may be something that someone with more experience may notice but i just cant.

  4. #4
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quick rule of thumb, keep opening and closing braces at the same distance to the right of your text document. Then tab each statement for every set of if statements, loops, and braces in your code that is associated with the block that goes with the if, loop, or brace.

  5. #5
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by mdrgon View Post
    Hello, i have been experiencing issues with a simple problem from SPOJ website. Sphere Online Judge (SPOJ) - Problem NDIGITS

    This is the code im working on.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void contador(short t[], int n)
    {
        int i;
        
         if(n == 1)
    
           t[0] = 10;
    
         else
         { 
           t[0] = 9;
               
           for (i = 0; i < n ;i++)
              t[i+1] = 0;
         }     
    }
    
    
    
    
    int main()
    {
        int n , i , d , m;
        short t[999];
        
        scanf(" %d ",&n);
        
        for(i = 0;i < n ;i++)
        {
          scanf(" %d ",&d);
          
            contador(t, d);
            
            if(n == 0)
             printf(" %d ", 0);
            else
              for (m = 0; m < d; m++)
               printf(" %d ", t[m]);
               
               
               printf(" \n ");
          
          
            
        }
        
       
     return 0;   
    }



    The solution itself isnt a big deal but given the amount of digit to look for it can become an issue.

    Basically i made an array of X amount of space (in this case 1000) and i filled it with a y amount of spaces with 0s, except the first space array[0] which will remain unchanged in (almost) all the cases.

    There is no way to save numbers with 50,60 or even a lot more in a variable so the only thing i could do was make an array and simply save every digit in a space and just print it out one by one. The problem is, according to the judge, the answer is wrong. I donīt know why, i want to, i keep changing it over and over again and now instead of programming im just trying to guess.

    Hope you guys can help me out here and thanks in advance.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    void contador(short t[], int n)
    {
        int i;
    
    
        if(n == 1)
            t[0] = 10;
        else
        {
            t[0] = 9;
    
    
            for (i = 0; i < n ;i++)
            t[i+1] = 0;
        }
    }
    
    
    
    
    
    
    
    
    int main()
    {
        int n , i , d , m;
        short t[999];
    
    
        scanf("%d",&n);
    
    
        for(i = 0;i < n ;i++)
        {
          scanf("%d",&d);
          contador(t, d);
    
    
          if(n == 0)
            printf(" %d ", 0);
          else
            for (m = 0; m < d; m++)
               printf("%d", t[m]);
          putchar('\n');
        }
    
    
    
    
     return 0;
    }
    Output :

    Code:
    1
    1
    10
    That's what the code looks like properly indented. And that's the input sample from the website I used. It looks like it follows the example. Are you getting any incorrect output, or what?
    Last edited by HelpfulPerson; 07-14-2013 at 09:03 PM.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    8
    I tried with the input example and it went accordingly. Hell i found a code that was approved by the judge, ran both, mine and the other code, and both (with the same input) outputs are the same.

  7. #7
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Well, I'm not quite sure I understand what the problem is then. If you get the same result with different methods, then you're both winners. I might could look at the 2nd person's code and see the differences and tell you how to change yours to about the same thing, but what would be the point of that? All I would be doing is getting you to copy someone's code just to get the same results again.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The problem asks: "How many n-digit whole numbers are there?"

    There are 10 1-digit whole numbers (0 to 9).
    There are 90 2-digit whole numbers (10 to 99).
    There are ....

    It's about spotting the pattern. The program is simple (much simpler than yours).

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Do you realize that when you put characters in your scanf() format specifier those characters must be entered as well. So by putting the spaces before and after the "%d" you are saying that those spaces will exist in your input. I doubt the online judge is entering those spaces in there test cases.

    Jim

  10. #10
    Registered User
    Join Date
    Jul 2013
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Do you realize that when you put characters in your scanf() format specifier those characters must be entered as well. So by putting the spaces before and after the "%d" you are saying that those spaces will exist in your input. I doubt the online judge is entering those spaces in there test cases.

    Jim
    I do. Sorry that i edited it out. I was sort of puzzled about the indentation and i hurried to separate everything.

    Quote Originally Posted by HelpfulPerson View Post
    Well, I'm not quite sure I understand what the problem is then. If you get the same result with different methods, then you're both winners. I might could look at the 2nd person's code and see the differences and tell you how to change yours to about the same thing, but what would be the point of that? All I would be doing is getting you to copy someone's code just to get the same results again.
    Exactly, the other way is efficient but i didnt think about it and just copying it wouldnt help at all. I wouldnt learn a thing after all.

    Quote Originally Posted by oogabooga View Post
    The problem asks: "How many n-digit whole numbers are there?"

    There are 10 1-digit whole numbers (0 to 9).
    There are 90 2-digit whole numbers (10 to 99).
    There are ....

    It's about spotting the pattern. The program is simple (much simpler than yours).
    That is the problem. And that is the pattern which i used (Not sure if you noticed it but ia ctually did). I coulda made it with a fewer lines of code, i can try that and see but meanwhile i will leave it like that.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by jimblumberg View Post
    Do you realize that when you put characters in your scanf() format specifier those characters must be entered as well. So by putting the spaces before and after the "%d" you are saying that those spaces will exist in your input. I doubt the online judge is entering those spaces in there test cases.

    Jim
    Not true. scanf treats spaces differently from other characters. A space stands for any amount of any whitespace, including none. However, the %d itself will first skip any whitespace, so " %d" and "%d" are equivalent.
    Code:
    // The following prints:
    //123,456
    
    #include <stdio.h>
    
    int main(void) {
        int n=0, m=0;
        char *str = "123\n456\n";
        sscanf(str, "   %d    %d   ", &n, &m);
        printf("%d,%d\n", n, m);
        return 0;
    }

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by mdrgon View Post
    that is the pattern which i used (Not sure if you noticed it but ia ctually did). I coulda made it with a fewer lines of code, i can try that and see but meanwhile i will leave it like that.
    What does your code give for 20?
    The answer should be
    90000000000000000000

  13. #13
    Registered User
    Join Date
    Jul 2013
    Posts
    8
    Quote Originally Posted by oogabooga View Post
    What does your code give for 20?
    The answer should be
    90000000000000000000
    The same. The answer from my code, as far as i know, is the right one.

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by mdrgon View Post
    The same. The answer from my code, as far as i know, is the right one.
    No it isn't! I just ran your code and it gave:
    Code:
     9  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
      0  0
    Not quite the same.
    Get rid of the extra spaces from your printf's. (I guess that's what jim was thinking of.)

    EDIT: I also just noticed in line 39 of the code in your original post, you presumably mean if (d==0).
    Last edited by oogabooga; 07-14-2013 at 09:52 PM.

  15. #15
    Registered User
    Join Date
    Jul 2013
    Posts
    8
    Ok.. So appearently there is an error in the problem. Appearently the judge input are above 1000 digit (I just increased the amount of spaces in the array and the judge accepted).

    So yeah... I really dont know what to say after trying so many things and realize this foolishness.

    This is the code that worked.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    
    
    int main()
    {
        int n , i , d , m;
        short t[10000];
        
        t[0]=0
        
        scanf("%d",&n);
        
        for(i=0;i<n;i++)
        {               
             scanf("%d",&d);
             if(d==1)
                     printf("%d", 10);
                     
             else if(d==0)
                     printf("%d", t[0]);
                     
             else
             {       
                     t[0]=9;
                     for(m=1;m<d;m++)
                     { 
                       t[m]=0;
                     }
             for(m=0;m<d;m++)
              printf("%d", t[m]);
             }
             printf("\n");
        }
        
        
     return 0;   
    }
    Last edited by mdrgon; 07-14-2013 at 10:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-15-2012, 02:28 PM
  2. Help needed with Puzzle solution provided in C
    By martin70 in forum C Programming
    Replies: 1
    Last Post: 11-22-2009, 07:36 PM
  3. [Solution Needed] Structures questions
    By yikchong in forum C Programming
    Replies: 3
    Last Post: 10-31-2009, 11:37 AM
  4. C Program solution needed...
    By vijay.neo in forum C Programming
    Replies: 6
    Last Post: 10-18-2006, 11:12 PM
  5. binary tree solution-- help needed
    By sanju in forum C Programming
    Replies: 3
    Last Post: 10-11-2002, 01:56 AM