100 - The 3n + 1 problem
Moderator: Board moderators
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
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
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)
Born from ashes - restarting counter of problems (800+ solved problems)
Why i always get compile error?
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
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
BEGIN - END marks
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!.
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!.
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
-- E. W. Dijkstra
-
- 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
//@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
/* @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
-
- 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)
/* @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);
}
}
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
-
- New poster
- Posts: 8
- Joined: Sat Jun 12, 2004 4:12 am
help me with 100
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.

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.

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.
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.

-
- New poster
- Posts: 8
- Joined: Sat Jun 12, 2004 4:12 am
I still have problem
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:
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:
-
- New poster
- Posts: 8
- Joined: Sat Jun 12, 2004 4:12 am
do I understand your meaning?
just now,I tested your code below:
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:
am I right?

Code: Select all
int main()
{
int a,b;
while ( scanf("%d %d",&a,&b) == 2 )
{
process_input();
}
return 0;
}
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;
}

-
- New poster
- Posts: 8
- Joined: Sat Jun 12, 2004 4:12 am
Ok ,I got it
Ok,I got it
and my code passed
Thank very very much!

and my code passed
Thank very very much!

What's wrong in it?
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??????
"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??????
