Hi everyone,
Please have a look at these 2 code. These are the solution of this problem,
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36...
code 1:
#include <stdio.h>
unsigned long findMaxCycleLength(unsigned long min, unsigned long max);
unsigned long findCycleLength(unsigned long n);
int main(int argc, char** argv)
{
unsigned long min, max, num = 0;
while(scanf("%li %li", &min, &max) != EOF)
{
printf("%li %li ", min, max);
if(min > max)
{
min^=max;
max^=min;
min^=max;
}
num = findMaxCycleLength(min, max);
printf("%li\n", num);
}
}
unsigned long findMaxCycleLength(unsigned long min, unsigned long max)
{
unsigned long temp, i;
unsigned long n = findCycleLength(max);
for(i = min; i < max; i++)
{
temp = findCycleLength(i);
if(temp > n)
{
n = temp;
}
}
return n;
}
unsigned long findCycleLength(unsigned long n)
{
if(n == 0) return 0;
unsigned long count = 1;
while(n != 1)
{
if(n%2 == 1)
{
n = 3*n+1;
}
else
{
n = n/2;
}
count++;
}
return count;
}
code 2:
#include <stdio.h>
int algorithm(int input)
{
int cycle ;
cycle = 1 ;
while( input!=1)
{
cycle++ ;
if(input%2)
input = (3*input) + 1 ;
else input = input/2 ;
} ;
return cycle ;
}
int main()
{
int i,j ;
int key, temp,temp2 ;
while((scanf("%d %d", &i, &j)))
{
temp = 0 ;
key = 0 ;
temp2 = i ;
for(i; i<=j ;i++)
{
temp = algorithm(i) ;
if(key <temp) key = temp ;
}
printf("\n%d %d %d", temp2, j, key) ;
}
return 0 ;
}
Last one is mine and when I compile it, i find Time Limit Exceeded. First
one is correct. Basic difference in my view of those is - in the first code,
max cycle is determined by comparing with *findCycleLength(max)* and in my
code I compared it with* temp = 0 *.
But I cannot fix what my prob is. Could you help ?
Thanx
--
Shamir Shakir Shishir
http://DewsWorld.t35.com
:::::.......... Hope never ends...........:::::
[Non-text portions of this message have been removed]