473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I am getting time limit exit

I am getting time limit exit ,please anyone can help me?
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3.  
  4. int main()
  5.  
  6.    {
  7.  
  8.  
  9.          int t,k;
  10.          long long i,j,r,x,temp,sum,num,count;
  11.  
  12.          scanf("%d",&t);
  13.          for(k=1;k<=t;k++)
  14.  
  15.             {
  16.  
  17.                 count=0;
  18.                 scanf("%lld %lld",&i,&j);
  19.  
  20.                  if(i>j)
  21.  
  22.                     {
  23.                       x=i;
  24.                       i=j;
  25.                       j=x;
  26.                     }
  27.  
  28.  
  29.                 for(num=i;num<=j;num++)
  30.  
  31.                  {
  32.  
  33.  
  34.                    temp=num;
  35.                    sum=0;
  36.  
  37.  
  38.                    while(temp)
  39.  
  40.                             {
  41.  
  42.  
  43.                                 r=temp%10;
  44.                                 sum=sum*10+r;
  45.                                 temp=temp/10;
  46.  
  47.                             }
  48.  
  49.                         if (sum==num)
  50.  
  51.                                       count++;
  52.  
  53.  
  54.                           }
  55.  
  56.                          printf("Case %d: %lld\n",k,count);
  57.  
  58.                       }
  59.  
  60.  
  61.                   return 0;
  62.  
  63.                }
Mar 30 '13 #1
8 1547
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 30 '13 #2
donbock
2,426 Expert 2GB
What values did you enter for t, i, and j to provoke the "time limit exit"?
Mar 31 '13 #3
sir it's online contests problem its time limit given 2s but my code take 2.996s
Apr 1 '13 #4
6
9645 9703
931 905
8002 1398
24152019045590870 350692834056785
21942154908112586 10788988012796340
6544883545356373 93161826011394755
Apr 1 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. 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.
Apr 1 '13 #6
donbock
2,426 Expert 2GB
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)?
Apr 1 '13 #7
donbock
2,426 Expert 2GB
@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.
Apr 2 '13 #8
weaknessforcats
9,208 Expert Mod 8TB
The subject was time exit and it has now shifted to palindromes.

Please continue the palindrome discussion on a new thread.
Apr 2 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Phillip Parr | last post by:
Is there any way I can impose a time limit on the file() function, say if a file doesn't open within a second I can skip opening it? Thanks...
4
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
2
by: User | last post by:
Anyone know of a function like raw_input(), but with a built-in or user-specified time limit? Ideally, it would return an empty string ('') and resume processing if the user didn't enter something...
0
by: mdanesh | last post by:
I have a VBA macro code that runs a SQL query (that is coppied from MS ACCESS) and it is using record set definition in VBA. It works fine, but it times out before it is done. I believe it is...
3
by: markon | last post by:
Hi, I would like to run few lines of code and limit the time. If the procedure would not be finished say in 1000ms I would like to stop processing it. Unfortunately I do not know where to...
9
by: jamesmcd999 | last post by:
I am currently developing a site and noticed that after about 2 minutes, I am being logged out of the members area. I have been trying to find a function that will allow me to specify the time...
1
by: woony | last post by:
I am making a webpage that gets his information from an as400 system. Now I seemed to hit some weird problem. I have for example this select: select field1,field2 from table this is over...
6
by: Rafe | last post by:
I tried to post some follow-ups to some issues I posted in the hopes of helping others, but I only get "reply to author" and "forward", but no "reply" option (using GoogleGroups). Is there some...
3
by: harita12 | last post by:
Hi I'm new to c# programming language, I'm making an app regarding online exam through paging, in which I want to show the time limit for the user and it decrease as the time passes & goes to next...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.