public static void main(String args[]) // entry point from OS
{
Main myWork = new Main(); // Construct the bootloader
myWork.run(); // execute
}
public void run() {
new ThreeNPlusOne().run();
}
class ThreeNPlusOne implements Runnable {
public void run() {
// My program here
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
// read the two integers
int i = in.nextInt();
int j = in.nextInt();
int maxLen = 0;
// send smaller integer first to maxCycleLen function
if (i > j) {
maxLen = maxCycleLen(j, i);
} else {
maxLen = maxCycleLen(i, j);
}
// print the input and max cycle length
System.out.printf("%d %d %d\n", i, j, maxLen);
}
System.exit(0);
}
/** Convenience function for checking if integer is even
* @param i
* @return boolean True if number is even
*/
boolean isEven(int i) {
if (i % 2 == 0)
return true;
else
return false;
}
/**
* Compute the max cycle length for any integer between i and j including the numbers i and j.
* For a number n, generate cycle as follows
* If number n is even then divide by 2
* If number n is odd then 3*n + 1
* Cycle ends when n = 1
*
* @param i first integer
* @param j second integer
* @return max cycle length
*/
int maxCycleLen(int i, int j) {
// Expect i < j
if ( i > j ) {
throw new AssertionError("first integer should be less than second integer.");
}
int maxLen = 0; // max cycle length
// start from first integer and work towards second integer
// compute cycle lengths for all integers in between
for(int p = i; p <= j; p++) {
int len = 1; // cycle len, starts with one as we'll always have 1 number in the cycle
int n = p; // don't change the iteration variable
// cycle ends when n becomes 1
while (n != 1 ) {
len++; // add one more to the cycle length
if (isEven(n)) {
n = n / 2;
} else {
n = 3*n + 1;
}
}
if (len > maxLen) {
// see if max cycle length changed
maxLen = len;
}
}
Hi,
I have been trying to submit my code for problem 100 - 3n + 1 and though the code works well when I run it on my pc, UVa shows me a runtime error.
My code is as follows:-
import java.io.*;
import java.util.*;
class Problem100
{
static int memo[] = new int[1000000];
static int ans = 0;
static int a;
static int b;
public static void main(String args[])throws IOException
{
Scanner str = new Scanner(System.in);
while(str.hasNext())
{
int a = str.nextInt();
int b = str.nextInt();
for(int i = 0; i < 1000000; i ++)
memo = -1;
memo[1] = 1;
int c = 0;
for(int i = a; i <= b; i ++)
{
if(compute(i) > c)
c = compute(i);
}
System.out.println(a + " " + b + " " + c);
}
System.exit(0);
}
public static int compute(int i)
{
if(memo != -1)
return memo;
else
{
ans = 0;
if((i % 2) == 1)
ans = 1 + compute(3 * i + 1);
else
ans = 1 + compute(i / 2);
memo = ans;
return ans;
}
}
}
I figured out that the class name needs to be "Main" for the program to work.
Also, I removed DP since the integer array memo of 1Million elements could have been a potential source of problem.
But still, UVa gives me "Wrong Answer".
Please help.
My new code -
import java.io.*;
import java.util.*;
class Main
{
//static int memo[] = new int[3000000];
//static int ans = 0;
static int a;
static int b;
static boolean swapped;
public static void main(String args[])throws IOException
{
swapped = false;
Scanner str = new Scanner(System.in);
while(str.hasNext())
{
int a = str.nextInt();
int b = str.nextInt();
//for(int i = 0; i < 3000000; i ++)
// memo = -1;
//memo[1] = 1;
int c = 0;
if(a > b)
{
c = a;
a = b;
b = c;
swapped = true;
}
c = 0;
for(int i = a; i <= b; i ++)
{
if(compute(i) > c)
c = compute(i);
}
if(swapped == true)
System.out.println(b + " " + a + " " + c);
else
System.out.println(a + " " + b + " " + c);
}
System.exit(0);
}
I've tried the sample input and I got the results as the sample output on the problem page.
I also checked that my program can handle both i >= j and i < j case, where i and j are the inputs.
But when I submit my code, the system gives me WAs. :'(
May someone help me for checking whether the output format is wrong.
Thx a lot!
#include <iostream>
using namespace std;
unsigned int method(unsigned int);
unsigned int maximumCycle(unsigned int, unsigned int);
int main(){
while(true){
unsigned int x, y;
cin >> x >> y;
if(cin.eof())
break;
cout << x << ' ' << y << ' ' << maximumCycle(x, y) << endl;
}
return 0;
}
unsigned int method(unsigned int target){
unsigned int n = target;
unsigned int count = 1;
do{
if(n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
count ++;
}while(n != 1);
return count;
}
unsigned int maximumCycle(unsigned int x, unsigned int y){
unsigned int start = x > y ? y : x;
unsigned int end = x > y ? x : y;
unsigned int length = end - start + 1;
unsigned int *cycle = new unsigned int[length];
for(unsigned int i = 0; i < length; i++){
cycle[i] = method(start + i);
}
unsigned int maximum = cycle[0];
for(unsigned int j = 1; j < length; j++){
maximum = cycle[j] > maximum ? cycle[j] : maximum;
}
return maximum;
}
Motivation for Reader usage instead of Scanner was, that for huge inputs Scanner usage ends in TLE while Reader does not and we do not know what to expect (now I know that you can use Scanner without TLE).