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

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

I think that cin.eof() works, but when after last case is new line character, you print additional line(s) of output (unnecessary, because it's not next test case ...)
Maybe it helps you

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
micheal
New poster
Posts: 1
Joined: Thu Apr 29, 2004 5:04 am

Why i always get compile error?

Post by micheal »

Hi all,
I dunn know why i always get compile error for problem 100( the 3n+1). The language i use is java and i run well in my comp.

Below is my source code,
[java]
import java.io.*;
import java.util.*;

class n1 {
public static void main(String[] args) throws IOException{
BufferedReader stdin = new
BufferedReader(new InputStreamReader(System.in));
String s;
int i,j;
int cycleLength1;
int cycleLength2;
StringTokenizer st;

while((s = stdin.readLine()) != null) {

st = new StringTokenizer(s," ");
i = Integer.parseInt(st.nextToken());
j = Integer.parseInt(st.nextToken());

cycleLength1 = computeCLength(i);
cycleLength2 = computeCLength(j);

if(cycleLength1 > cycleLength2 ) {
System.out.println(i+" "+j+" "+cycleLength1);
}
else {
System.out.println(i+" "+j+" "+cycleLength1);
}
}//end while

}//end main

static int computeCLength(int n) {

int cycleLength = 0;

while(n != 1) {

// integer i is even number
if(n%2 == 0) {
n = n/2;
++cycleLength;
}

// integer i is odd number
else {
n = 3*n + 1;
++cycleLength;
}
}//end while

return ++cycleLength;
}//end computeCLength


}//end n1

[/java]

thx for ur explanation

mike
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

There are instructions on the main page for the format needed for Java. It's been awhile since I coded it in Java, but you need to name the class Main and other stuff.

What is the error message?
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

I noticed that you used BufferedReader. Over here in the acm online judge, that would be a restricted function as well. You have to use the method for reading input using Java which can be found in the instructions on the main page.
locokevin
New poster
Posts: 1
Joined: Tue Apr 27, 2004 12:09 am
Location: LIMA
Contact:

BEGIN - END marks

Post by locokevin »

If your mail system adds extra lines at the end of your letter (as Hotmail or Yahoo do), you'll get a compile error. A way to skip this problem is to place a "@END_OF_SOURCE_CODE" or a "@end_of_source_code" message in a single line just after the last line of source code. Do not place any source code in this line!.

If your mail system adds extra lines in the first body lines of your letter, you'll also get a compile error. A way to skip this problem is to place a "@BEGIN_OF_SOURCE_CODE" or a "@begin_of_source_code" message in a single line before the first line of source code. Do not place any source code in this line!.
Kentaro
New poster
Posts: 19
Joined: Thu Feb 05, 2004 4:41 am
Location: Canada, eh?

Post by Kentaro »

I find when I'm compiling C++ programs to test them before submitting that there are some libraries that my implementation includes implicity (i.e. I don't have to use #include to get them) but they need to be explicitly included when the judge is compiling the program. I try to be really careful with what I #include to make sure everything I've used is there.
Computer Science is no more about computers than Astronomy is about telescopes.
-- E. W. Dijkstra
Yousuf Shetu
New poster
Posts: 5
Joined: Fri May 07, 2004 7:00 pm
Location: London
Contact:

Damn Compile error in prb 100 if I use Java

Post by Yousuf Shetu »

//@BEGIN_OF_SOURCE_CODE
/* @JUDGE_ID: 45460CH 100 Java "The 3n+1 problem" */

import java.io.*;
public class nProblem
{
static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;

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

return (new String (lin, 0, lg));
}

public static void main(String args[])
{
int n=0,a,b,k=0,count=0;
int[] array;
int currentMax=0;
String str = " ";
nProblem np = new nProblem();
array = new int[200];
System.out.println("Enter 2 integers (1st one > last one): ");
String input = nProblem.ReadLn (255);
a = Integer.valueOf(input).intValue();

String input1 = nProblem.ReadLn (255);
b = Integer.valueOf(input1).intValue();

for(int i=a;i<=b;i++)
{
n = i;
count = 0;
for(int j=1;;j++)
{
count++;
if (n ==1)
break;
else if(n % 2 != 0)
n = ((3*n)+1);
else
n = n/2;
}
array[k]=count;
k++;
}

for(int j=array.length-1;j>=0;j--)
{
if(currentMax < array[j])
{
currentMax = array[j];
}
}
System.out.println(currentMax);
}
}
//@END_OF_SOURCE_CODE
yusufshetu
Yousuf Shetu
New poster
Posts: 5
Joined: Fri May 07, 2004 7:00 pm
Location: London
Contact:

Still Compile error in problem 100 in Java (Pls help me)

Post by Yousuf Shetu »

/* @JUDGE_ID: 45460CH 100 Java "The 3n+1 problem" */

import java.io.*;
class nProblem
{
static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;

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

return (new String (lin, 0, lg));
}

public static void main(String args[])
{
int n=0,a,b,k=0,count=0;
int[] array;
int currentMax=0;
String str = " ";
nProblem np = new nProblem();
array = new int[200];
String input = nProblem.ReadLn (255);
a = Integer.valueOf(input).intValue();

String input1 = nProblem.ReadLn (255);
b = Integer.valueOf(input1).intValue();

for(int i=a;i<=b;i++)
{
n = i;
count = 0;
for(int j=1;;j++)
{
count++;
if (n ==1)
break;
else if(n % 2 != 0)
n = ((3*n)+1);
else
n = n/2;
}
array[k]=count;
k++;
}

for(int j=array.length-1;j>=0;j--)
{
if(currentMax < array[j])
{
currentMax = array[j];
}
}
System.out.println(a +" "+b +" "+currentMax);
}
}
yusufshetu
Tourskey_911
New poster
Posts: 8
Joined: Sat Jun 12, 2004 4:12 am

help me with 100

Post by Tourskey_911 »

I'm a new comer here,when I first come about the 100-The 3n + 1 problem

It's really a very easy case,but what I wanted to know is its input.

its input is like this:

1 10
100 200
201 210
900 1000

I don't know how many pairs of integers should I input

or there is a rule to stop input,like "-1"?

So that's all I want to ask.
:-?
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

Hi,

Well you never mentioned the Language you use, but if it is C/C++, you could use

[c]
int main()
{
int a,b;
while ( scanf("%d %d",&a,&b) == 2 )
{
process_input();
}
return 0;
}
[/c]

Notice the ==2 in the while condition, this means that you are expecting 2 numbers( or any other data type), as input. Now the judge program will give data from a file, so when the end of file is encountered, the condition will be false and the loop will terminate.:wink:
Tourskey_911
New poster
Posts: 8
Joined: Sat Jun 12, 2004 4:12 am

I still have problem

Post by Tourskey_911 »

Oh,Thank u.

But I think you misunderstanded me,

yes,if I want to input 2 pairs of datas,I can do it like this:

Code: Select all

int n=2;
for(int i=0;i<n;i++)
{
cin>>a>>b;
}

but I don't know how many pairs did the system need,

That's to say I don't know "n",

so I have no idea of programming.

forgive my poor english. :roll: 


Tourskey_911
New poster
Posts: 8
Joined: Sat Jun 12, 2004 4:12 am

do I understand your meaning?

Post by Tourskey_911 »

just now,I tested your code below:

Code: Select all

int main()
{
int a,b;
while ( scanf("%d %d",&a,&b) == 2 )
{
process_input();
}
return 0;
}
I got it,you told me the system use datas in a file to test my code,right?

That's to say I don't need to pay attention to "n",if the datas in the file ara

all tested,the programme will stop.

so I think this code will do the same work:

Code: Select all

int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		process_input();
	}
	return 0;
}
am I right?

:D
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

Yes that is correct.

You could also use
[cpp]
while ( cin>>a>>b && !cin.eof() )
{

}
[/cpp]

[/cpp]
Tourskey_911
New poster
Posts: 8
Joined: Sat Jun 12, 2004 4:12 am

Ok ,I got it

Post by Tourskey_911 »

Ok,I got it

and my code passed

Thank very very much!

:D
Austin
New poster
Posts: 4
Joined: Sat Jun 26, 2004 9:29 am
Location: Bangladesh

What's wrong in it?

Post by Austin »

Everytime i'm receiving this Wrong Answer reply including
"Your program has not solved the problem. It ran during 0.002 seconds."

#include<stdio.h>
#include<stdlib.h>


int function(int k);
int count;
void main()
{
int m,n,i,temp,maxCount,tempCount;

scanf("%d %d",&n,&m);


if(n>m)
{
temp=n;
n=m;
m=temp;

}

maxCount=function(n);
for(i=n+1;i<=m;i++)
{
tempCount=function(i);
if(maxCount<tempCount)
{ temp=tempCount;
tempCount=maxCount;
maxCount=temp;
}


}

printf("\n %d %d %d",n,m,maxCount);

}


int function(int k)

{ count=1;

while(k!=1)
{ count++;
if((k%2)==0)
k=k/2;
else
k=3*k+1;
}

return count;

}

But compiler in my pc giving the required result.
so, What's the problem in it?????? :-?
Post Reply

Return to “Volume 1 (100-199)”