#include <stdio.h>
#include <map>
#define MIN(x,y) (x<y)?x:y
#define MAX(x,y) (x>y)?x:y
std::map<unsigned long long int,unsigned long long int> number;
unsigned long long int compute(unsigned long long int n)
{
if(!number[n])
{
if(n&1)
number[n] = compute(3*n+1) + 1;
else
number[n] = compute(n>>1) + 1;
}
return number[n];
}
int main()
{
number[1] = 1;
int i,j,temp_i,temp_j;
unsigned long long int max;
unsigned long long int temp;
int line = 0;
while(scanf("%d %d",&temp_i,&temp_j)!=EOF)
{
if(line&1)
printf("\n");
max = 0;
i=MIN(temp_i,temp_j);
j=MAX(temp_i,temp_j);
for(; i<=j ; i++)
{
temp = compute(i);
if(temp>max)
max = temp;
}
printf("%lu %lu %lu",temp_i,temp_j,max);
line=1;
}
return 0;
}
Thanks in advanced for any assistance. When i submit my code i get a run time error, specifically "Your submission with number 12818442 for the problem 100 - The 3n + 1 problem has failed with verdict Runtime error.This means that the execution of your program didn't finish properly. Remember to always terminate your code with the exit code 0." Could anyone look look at my code and tell me where the problem lies? It works fine in the testing i did. I am using the latest version of netbeans to code. Thanks for any help and/or advice.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package onlinejudge100;
import java.util.Scanner;
/**
*
* @author Dave
*/
public class Main {
//method to conduct the 3n + 1 problem
float cycleCount(float i)
{
float count = 1; //resets count to 1 every time the method is called to include the number 1 :NOTE if count is not set to one, the previous value will be stored and it will continue to add up.
float n = i; //sets N to the arguement sent to through the method call.
while(n != 1)
if (n % 2 == 1) //inspects the number to see if it is odd
{
n = 3 * n + 1; //formula
count = count+1; //counts the occurance
}
else
{
n = n / 2; //formula
count = count+1; //counts the occurance
}
return count; //returns count to the object call
}
public static void main(String[] args)
{
Main cycle = new Main(); //creates new object of class OnlineJudge100
Scanner in = new Scanner(System.in); //creats object of class Scanner to take in inputs
float count = 0;
float i=0, j=0, answer = 0; //variable declarations
System.out.print("Enter Integer I and J: "); //output message
i = in.nextInt(); //sets value to user input
if (i < 0 || i > 999999) //restricts number between 0 and 1,000,000
throw new IllegalArgumentException("Specified number is out of range"); //throws error if restriction is not met
j = in.nextInt(); //sets value to user input
if (j < 0 || j > 999999) //restricts number between 0 and 1,000,000
throw new IllegalArgumentException("Specified number is out of range"); //throws error if restriction is not met
for (float test = i; test < j + 1; test++) //for loot to test all the numbers between the 2 input integers.
{
float result = cycle.cycleCount(test); //calls the method in the object and sets the result to variable result
count = count + 1;
//System.out.println("result = " + count);
if (result > answer) //compares result to stored answer, if result is bigger, save that into the answer
{
answer = result;
}
}
System.out.println(i + " " + j + " " + answer); //output information
}
}
yea ive been trying this problem for the tenth time already and its always it wrong answer or runtime. ive check it and it seems fine but still...WA. can anyone tell me whats wrong?
Hey guys, haven't coded in a long time and trying to get started on uva to freshen up. Forgotten almost everything, so pardon any noob mistakes. Wondering why my code is giving WA?