100 - The 3n + 1 problem
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: time limit excede problem
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
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
Check input and AC output for thousands of problems on uDebug!
Re: time limit excede problem
Thanks for your advice
Re: If you get WA in problem 100, read me before post!
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?
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?
Re: If you get WA in problem 100, read me before post!
Nevermind, I simply wasn't returning 0. It works ![:roll:](./images/smilies/icon_rolleyes.gif)
![:roll:](./images/smilies/icon_rolleyes.gif)
100 Runtime Error
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;
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 100 Runtime Error
What happens if i>j?
Check input and AC output for thousands of problems on uDebug!
Newbie can't get AC
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.
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.
Newbie can't get AC
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
I tried the sample program but than got TLE and RTE.
I like to know what I'm doing wrong in my submissions.
Thanks
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Re: If you get WA in problem 100, read me before post!
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));
}
}
}
-
- 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!
Don't use a package
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 11
- Joined: Sun Dec 09, 2012 11:18 pm
Re: If you get WA in problem 100, read me before post!
now getting WA..:/
-
- 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!
Input:AC output:
Code: Select all
1 1
Code: Select all
1 1 1
Check input and AC output for thousands of problems on uDebug!
Re: If you get WA in problem 100, read me before post!
AC
Last edited by RoniphpBB on Sat Jun 15, 2013 11:45 pm, edited 1 time in total.
-
- 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!
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) {
...
}
while(cin >> i >> j) {
...
}
Check input and AC output for thousands of problems on uDebug!
input error in 3n+1 problem
Hi everybody,
I was trying to solve 3n+1 exercise . I don't know how to get input.
can anybody help?
I was trying to solve 3n+1 exercise . I don't know how to get input.
can anybody help?