Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Last active August 29, 2015 14:07
Show Gist options
  • Save henrybear327/84b216ec3d949459e79c to your computer and use it in GitHub Desktop.
Save henrybear327/84b216ec3d949459e79c to your computer and use it in GitHub Desktop.
UVA100.c
/*
READ THE QUESTIONS CAREFULLY!
EVERY WORD REGARDING ALGORITHM, INPUT, OUTPUT, ETC. MUST BE CAREFULLY READ AND FOLLOWED.
eg. Think of swapping the input; be cautious on the output format!!!
What the statements said in the questions count, not my personal judgement!
The f****** "output order must be the same as input" statement caught me off guard TWICE! Learn the lesson.
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int input_lower_end, input_upper_end;
while(scanf("%d %d", &input_lower_end, &input_upper_end) != EOF)
{
int initial_input_lower_end = input_lower_end, initial_input_upper_end = input_upper_end;
if(input_lower_end > input_upper_end) /*check and swap numbers if needed*/
{
int temp = 0;
temp = input_lower_end;
input_lower_end = input_upper_end;
input_upper_end = temp;
}
int counter = input_lower_end, cycle = 0;
for(counter = input_lower_end; counter <= input_upper_end; counter++) /*check numbers between the lower and upper limits*/
{
int quotient = counter, cycle_temp = 0;
while(quotient != 1)
{
if(quotient % 2 != 0)
quotient = 3 * quotient + 1;
else
quotient /= 2;
cycle_temp++;
}
cycle_temp++; /* if quotient != 1, this step still counts in the cycle*/
cycle = (cycle >= cycle_temp) ? cycle : cycle_temp; /*a good idea to use it*/
}
printf("%d %d %d\n", initial_input_lower_end, initial_input_upper_end, cycle);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment