Page 32 of 93

Posted: Tue Apr 27, 2004 2:17 pm
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

Why i always get compile error?

Posted: Thu Apr 29, 2004 5:26 am
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

Posted: Thu Apr 29, 2004 5:29 am
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?

Posted: Thu Apr 29, 2004 7:40 am
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.

BEGIN - END marks

Posted: Tue May 04, 2004 7:55 am
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!.

Posted: Wed May 05, 2004 4:58 pm
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.

Damn Compile error in prb 100 if I use Java

Posted: Fri May 07, 2004 7:19 pm
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

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

Posted: Sat May 08, 2004 11:51 am
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);
}
}

help me with 100

Posted: Sun Jun 20, 2004 10:45 am
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.
:-?

Posted: Sun Jun 20, 2004 10:58 am
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:

I still have problem

Posted: Mon Jun 21, 2004 6:48 am
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: 



do I understand your meaning?

Posted: Mon Jun 21, 2004 7:05 am
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

Posted: Mon Jun 21, 2004 11:49 am
by shamim
Yes that is correct.

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

}
[/cpp]

[/cpp]

Ok ,I got it

Posted: Mon Jun 21, 2004 12:37 pm
by Tourskey_911
Ok,I got it

and my code passed

Thank very very much!

:D

What's wrong in it?

Posted: Sat Jun 26, 2004 10:15 am
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?????? :-?