I am getting time limit exit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • b07031992
    New Member
    • Mar 2013
    • 3

    I am getting time limit exit

    I am getting time limit exit ,please anyone can help me?
    Code:
    #include <stdio.h>
    
    
    int main()
    
       {
    
    
             int t,k;
             long long i,j,r,x,temp,sum,num,count;
    
             scanf("%d",&t);
             for(k=1;k<=t;k++)
    
                {
    
                    count=0;
                    scanf("%lld %lld",&i,&j);
    
                     if(i>j)
    
                        {
                          x=i;
                          i=j;
                          j=x;
                        }
    
    
                    for(num=i;num<=j;num++)
    
                     {
    
    
                       temp=num;
                       sum=0;
    
    
                       while(temp)
    
                                {
    
    
                                    r=temp%10;
                                    sum=sum*10+r;
                                    temp=temp/10;
    
                                }
    
                            if (sum==num)
    
                                          count++;
    
    
                              }
    
                             printf("Case %d: %lld\n",k,count);
    
                          }
    
    
                      return 0;
    
                   }
    Last edited by acoder; Mar 31 '13, 11:55 AM.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Wha do you mean by time limit exit?

    In this code there are no comments of any kind. Nothing that gives a clue as to what the code is for.

    I don't have time to do debugging so I suggest you step through your code using your debugger.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      What values did you enter for t, i, and j to provoke the "time limit exit"?

      Comment

      • b07031992
        New Member
        • Mar 2013
        • 3

        #4
        sir it's online contests problem its time limit given 2s but my code take 2.996s

        Comment

        • b07031992
          New Member
          • Mar 2013
          • 3

          #5
          6
          9645 9703
          931 905
          8002 1398
          241520190455908 70 350692834056785
          219421549081125 86 107889880127963 40
          654488354535637 3 931618260113947 55

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Execution speed is based on yur computer, the operating system on it, the compiler that was used, the settings on the compiler(debug? release? optimized?). Measuring execution time on my machine almost certainly will not match your time.

            You say this is a contest. Do you know if the judges machine is identical to yours?

            You can tweak the code by changing all var++ to ++var because that's faster.

            Code:
            sum=sum*10+r; is slower than sum*= 10+r;
            Avoid divide. Do you really need to divide by 10? Would 8 work? If so, bit shift right to divide by 8 Ditto for multiply. A left shift 4 bits is a multiply by 8. Bit operarons are vastly faster than divide/multiply.

            Remove long long int. That's slower than int.

            Good luck.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              You have loops nested three deep. That will certainly contribute to the execution time of the program. However, I can't offer any suggestions without understanding what this program needs to accomplish. Please explain.
              I don't understand how to interpret the numbers you provided in post #5.

              What circumstances do you believe will cause the if test on line 49 to fail (preventing you from incrementing count on line 50)?

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                @b07031992, I received email notification that you had posted a reply. Since your reply has disappeared, I am responding to what the notification email says you said.

                Your intention is to determine how many palindromic numbers occur within some particular range (0 to 10^17); and it appears from your code that you are trying to do this by iterating across that range and testing each value to see if it is a palindrome.

                A couple of comments:
                1. This is bound to be a time-inefficient algorithm because the vast majority of the values you test will not be palindromes.
                2. I don't think your logic is correct: it seems to me that lines 34-51 will increment count for all nonzero values of num, rather than for only those values of num that are palindromes.

                Hint: shift your thinking away from testing whether integers are palindromes towards thinking about the rules for generating all the palindromic strings of digits with length n. In your case, n ranges from 1 to 16.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  The subject was time exit and it has now shifted to palindromes.

                  Please continue the palindrome discussion on a new thread.

                  Comment

                  Working...