100 - The 3n + 1 problem

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: time limit excede problem

Post by brianfry713 »

Don't read from a file. You can see a sample solution to problem 100 at:
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
Check input and AC output for thousands of problems on uDebug!
hercules
New poster
Posts: 2
Joined: Thu Oct 25, 2012 8:23 pm

Re: time limit excede problem

Post by hercules »

Thanks for your advice
magnetik
New poster
Posts: 2
Joined: Tue Nov 27, 2012 9:43 pm

Re: If you get WA in problem 100, read me before post!

Post by magnetik »

My answer to problem 100 is pasted here on ideaone.

As you can see from the input and output, the program works as expected.

However, using the online judge I get "runtime error."

What is the problem?

Edit:

Ok, based on reading some previous posts I have made my program handle reversed ranges.

Updated code is here http://ideone.com/HK0ugg

However, I still get "runtime error." What is the problem?
magnetik
New poster
Posts: 2
Joined: Tue Nov 27, 2012 9:43 pm

Re: If you get WA in problem 100, read me before post!

Post by magnetik »

Nevermind, I simply wasn't returning 0. It works :roll:
Fedaykin
New poster
Posts: 6
Joined: Sun Dec 09, 2012 9:27 pm

100 Runtime Error

Post by Fedaykin »

Code: Select all

import java.util.*;

class Main {

	public static void main(String[] args){
		new Main().solveProblem();
	}

	public void solveProblem(){
		final Scanner in = new Scanner(System.in);
		while(in.hasNextInt()){
			int i = in.nextInt();
			int j = in.nextInt();
			int[] values = new int[j-i+1];
			for(int k = i;k <= j;k++){
				values[k-i] = findSeqNum(k);
			}
			int most = getMax(values,j-i+1);
			System.out.println(""+i+" "+j+" "+most);
		}
		in.close();
	}

	public int findSeqNum(int n){
		int count = 1;
		while(n != 1){
			if(n%2 == 0)
				n = n / 2;
			else
				n = 3 * n + 1;
			count++;
		}
		return count;
	}

	public int getMax(int[] vals,int size){
		int max = vals[0];
		for(int i = 0;i < size;i++)
			if(vals[i] > max)
				max = vals[i];
		return max;
	}
}
So what am I doing wrong here?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 100 Runtime Error

Post by brianfry713 »

What happens if i>j?
Check input and AC output for thousands of problems on uDebug!
Tiny
New poster
Posts: 2
Joined: Thu Dec 13, 2012 12:20 am

Newbie can't get AC

Post by Tiny »

Hello i am new to the UVA online Judge.
I copied the sample program that is a solution to problem 100.
But I get a compilation error ewvery time i submit that program.

This is the sample i try to get AC i found it on the site of UVA
What can be wrong?

program p100 ;
var
i, j: integer;
function getCL(N: integer): integer;
var k: integer;
begin
k := 1;
while N <> 1 do begin
if odd(N) then N := 3*N + 1
else N := N div 2;
k := k + 1;
end;
getCL := k;
end;
function getMaxCL(i, j: integer): integer;
var k: integer;
max, curCL: integer;
begin
max := 0;
for k:=i to j do begin
curCL := getCL(k);
if curCL > max then max := curCL;
end;
getMaxCL := max;
end;
begin
{$IFNDEF ONLINE_JUDGE}
assign(input, 'input.txt');
reset(input);
assign(output, 'output.txt');
rewrite(output);
{$ENDIF}
while not eof(input) do begin
readln(i, j);
write(i, ' ', j, ' ');
if i < j then
writeln(getMaxCL(i, j))
else
writeln(getMaxCL(j, i));
end;
{$IFNDEF ONLINE_JUDGE}
close(input);
close(output);
{$ENDIF}
end.
Tiny
New poster
Posts: 2
Joined: Thu Dec 13, 2012 12:20 am

Newbie can't get AC

Post by Tiny »

Can anyone post a program for problem 100 then got AC.
I tried the sample program but than got TLE and RTE.
I like to know what I'm doing wrong in my submissions.

Thanks
farnaws123
New poster
Posts: 11
Joined: Sun Dec 09, 2012 11:18 pm

Re: If you get WA in problem 100, read me before post!

Post by farnaws123 »

Code: Select all

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample_java;

/**
 *
 * @author Nawshad
 * Problem: 100
 */
// 

import java.io.IOException;
import java.util.StringTokenizer;

class Main
{
    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
                
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String(lin, 0, lg));
    }

    int loop(int i)
    {
        int n=i;
        int count=1;
        while(n>1)
        {
            if(n%2==0)
            {
                n=n/2;
            }
            else
            {
                n=3*n+1;
            }
            count++;
        }
       return count; 
    }

    int max_cycle(int j,int k)
    {

        int max=-1;
        for(int i=j;i<k;i++)
        {
            int count=loop(i);
            if(count>max)
            {
                max=count;
            }
        }
        return max;
    }

    
    
    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }

    void Begin()
    {
        String input;
        StringTokenizer idata;
        int a, b, min, max;

        while ((input = Main.ReadLn (255)) != null)
        {
          idata = new StringTokenizer (input);
          a = Integer.parseInt (idata.nextToken());
          b = Integer.parseInt (idata.nextToken());
          if (a < b) { min=a; max=b; } else { min=b; max=a; }
       
         System.out.println (a + " " + b + " " + max_cycle(min, max));
        }
          
    }
    
    
}
What could be the possible reason for RE for that code?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: If you get WA in problem 100, read me before post!

Post by brianfry713 »

Check input and AC output for thousands of problems on uDebug!
farnaws123
New poster
Posts: 11
Joined: Sun Dec 09, 2012 11:18 pm

Re: If you get WA in problem 100, read me before post!

Post by farnaws123 »

now getting WA..:/
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: If you get WA in problem 100, read me before post!

Post by brianfry713 »

Input:

Code: Select all

1 1
AC output:

Code: Select all

1 1 1
Check input and AC output for thousands of problems on uDebug!
RoniphpBB
New poster
Posts: 10
Joined: Sat Feb 09, 2013 11:22 am

Re: If you get WA in problem 100, read me before post!

Post by RoniphpBB »

AC
Last edited by RoniphpBB on Sat Jun 15, 2013 11:45 pm, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: If you get WA in problem 100, read me before post!

Post by brianfry713 »

There is more than one case in the sample input and output. You should continue reading i and j until there are no more. Do a loop like:
while(cin >> i >> j) {
...
}
Check input and AC output for thousands of problems on uDebug!
sasy222
New poster
Posts: 1
Joined: Tue Feb 12, 2013 7:37 am

input error in 3n+1 problem

Post by sasy222 »

Hi everybody,

I was trying to solve 3n+1 exercise . I don't know how to get input.
can anybody help?
Post Reply

Return to “Volume 1 (100-199)”