I have written the solution code of Jolly Jump problem (ACM 10038 uva).
My Code is as follows:
#include<stdio.h>
#include<stdlib.h>
int main(){
int count=0;
int Number[3000]={0};
int Absolute[3000]={0};
bool flag=true;
while(scanf("%d",&count)){
for(int i=0;i<count;++i){
scanf("%d",&Number[i]);
Absolute[i]=0;
}
for(int j=0;j<count-1;++j){
int diff=Number[j]-Number[j+1];
if(diff<0)diff*=-1;
Absolute[diff]=1;
}
flag=true;
for(int x=1;x<count;++x){
if(Absolute[x]!=1){
flag=false;
break;
}
}
if(flag)printf("Jolly\n");
else printf("Not Jolly\n");
}
return 0;
}
But the conmmited result is Time limit exceeded. Why? How do I revise my code to lower the run time?
Your program is probably exceeding the time limit because it never finishes. If/when
scanf()returnsEOF, the following will never stop looping:In these online programming problems, it’s usually a good idea to at least run your proposed solution against the example data they provide in the question and see if you get the expected output. If your program doesn’t produce the expected output, then you know you have a problem to fix (and you have something concrete to debug).