Note that there are some explanatory texts on larger screens.

plurals
  1. POmy solution for (3n+1) is exceeding 3 sec time limit
    primarykey
    data
    text
    <p>here i have written a program on (3n+1) problem.it's also a problem from UVa. I believe it's a known problem.but when i am going to submit it in that online judge community it is sending me a <strong>Time Exceeding Error</strong>. the time limit is 3 sec. I have done what my little knowledge can do. If anyone can help me with some more advice I would be glad. my code is:</p> <pre><code>#include &lt;iostream&gt; #include&lt;stdlib.h&gt; #include&lt;stdio.h&gt; using namespace std; int main() { int loop=1; unsigned short *cyclelength; while(loop=1){ unsigned int x,y,i,j,num,count=0,p,k,c,max; for(;;){ cout&lt;&lt;"enter two integers. they must not be equal and must be between 1 and 1000000\n"; while(!(cin&gt;&gt;i&gt;&gt;j)){ cout&lt;&lt;"please enter a number\n"; cin.clear(); cin.ignore(1000,'\n'); } if(i&gt;=1 &amp;&amp; i&lt;1000000 &amp;&amp; j&gt;=1 &amp;&amp; j&lt;1000000 &amp;&amp; i!=j){ break; } else{ printf("try the whole process again\n"); } } if(i&gt;j){ x=i; y=j; } else{ x=j; y=i; }/*making x always greater than y*/ cyclelength=(unsigned short *)malloc(1000000 *sizeof(unsigned short)); if (NULL==cyclelength){ printf("process aborted"); return 0; } else{ /*solution part for the range of number. and solution for each number put into cyclelength.*/ num=y; while(num&lt;=x){ p=1; k=num; while(k!=1){ if(k%2==0) k=k/2; else k=3*k+1; p+=1; } cyclelength[count]=p; num+=1; count+=1; } c=0; max=cyclelength[c]; for(c=0;c&lt;x-y-1;c+=1){ if(max&lt;cyclelength[c+1]){ max=cyclelength[c+1]; } } free(cyclelength); cyclelength = NULL; cout&lt;&lt;i&lt;&lt;" "&lt;&lt;j&lt;&lt;" "&lt;&lt;max&lt;&lt;'\n'; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload