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

ergysr
New poster
Posts: 9
Joined: Sun Oct 07, 2007 1:25 pm

Need Help, I'm New..

Post by ergysr »

Hi all,
i just registered and made my first submission(3n+1) on the online judge, but i got WA. I'm quite sure my algorithm is correct, but i think my problem is input & output. Should i input values via input file(what' the name of that file) or values are entered manually? What about the output? A file or just a writeln function? Somebody clarify me please...
Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko »

You read input from standard in and write to standard out.
ergysr
New poster
Posts: 9
Joined: Sun Oct 07, 2007 1:25 pm

Post by ergysr »

Darko wrote:You read input from standard in and write to standard out.
Thnx darko, read all test cases and then output results? or just input output, input output?
Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko »

You can output result of each test case after you process it. Or you can do it all at once. It doesn't really matter, what they do is redirect your output to a file and then compare with their results (or run a program to check it).

I am guessing you are using Pascal, so I can't really help you much, but they posted a solution to 100, maybe that can help:
http://online-judge.uva.es/problemset/data/p100.p.html
ergysr
New poster
Posts: 9
Joined: Sun Oct 07, 2007 1:25 pm

Post by ergysr »

Darko wrote:You can output result of each test case after you process it. Or you can do it all at once. It doesn't really matter, what they do is redirect your output to a file and then compare with their results (or run a program to check it).

I am guessing you are using Pascal, so I can't really help you much, but they posted a solution to 100, maybe that can help:
http://online-judge.uva.es/problemset/data/p100.p.html
thanks darko, really thanks.
Hatty
New poster
Posts: 1
Joined: Thu Oct 18, 2007 7:14 am

Post by Hatty »

Getting wrong answer, I dont know why...

Edit: Code removed. Thanks sapnil, didnt have to use long int though, as the challenge says that all numbers are below 1mil

It works, but I don't know why I get wrong answer...

Code: Select all

hatty@study:~/dev/uva$ ./100
1 10
1 10 20
100 200
100 200 125
201 210
201 210 89
900 1000
900 1000 174

hatty@study:~/dev/uva$ 
Those are the same inputs and outputs as in the problem.. Help?
Last edited by Hatty on Thu Nov 08, 2007 7:13 am, edited 1 time in total.
sumonSUST
New poster
Posts: 5
Joined: Fri Oct 26, 2007 3:55 pm

TLE help

Post by sumonSUST »

I get TLE plz help me

Code: Select all

//Removed
Thanks
sumon
Last edited by sumonSUST on Fri Oct 26, 2007 4:09 pm, edited 1 time in total.
sapnil
Experienced poster
Posts: 106
Joined: Thu Apr 26, 2007 2:40 pm
Location: CSE-SUST
Contact:

Post by sapnil »

To sumon

Just change this line:

Code: Select all

while(n!=1)
to
while(n>1)
Thanks
Keep posting
Sapnil
"Dream Is The Key To Success"

@@@ Jony @@@
sumonSUST
New poster
Posts: 5
Joined: Fri Oct 26, 2007 3:55 pm

Post by sumonSUST »

To sapnil

Thanks.
I get ACC now

Sumon
sapnil
Experienced poster
Posts: 106
Joined: Thu Apr 26, 2007 2:40 pm
Location: CSE-SUST
Contact:

Post by sapnil »

To Hatty

Your problem is in taking input.
Make it simple

Code: Select all

while(scanf("%ld %ld",&j,&i)==2)
{

}
>> use long data type
>> remove your code after Acc

Thanks
Keep postingh
sapnil
"Dream Is The Key To Success"

@@@ Jony @@@
think12381
New poster
Posts: 1
Joined: Sun Nov 18, 2007 4:20 pm

about ACM 100 WA

Post by think12381 »

Dear all
I test some numbers to my code but no error can't be finded!!
Send this code to Online Judge, it always tells me "WA".
May you tell me what WA is happened on my code ???

Thank you!


#include <stdio.h>

long algorithm(long );
void swap(long *,long*);

int main(int argc, char *argv)
{
long i,j,first=0,second=0,temp=0,n=0;

while(scanf("%ld%ld",&i,&j)==2)
{
if((i<=0) || (j<=0) || (i>1000000) || (j>1000000))
{
printf("the two number is error!!\n");
exit(1);
}

if((i==1) && (j==1))
{
temp=1;
}
else
{
if(j<i)
swap(&i,&j);

temp = algorithm(i);
i=i+1;
for(i;i<j;i++)
{

second=algorithm(i);

if(temp<second)
temp=second;
}
}
printf("MAX cycle length is =%d\n",temp);
}

return 0;
}

long algorithm(long n)
{
int time=1;

do
{
if((n%2)==0)
{
n=n/2;
}
else
{
n=n*3+1;
}
time++;
}while(n!=1);

return time;
}

void swap(long *i,long*j)
{
long c;

c=*j;
*j=*i;
*i=c;
}
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

You should strictly follow the sample output, as described in the section "The Output" of the problem statement. Essentially, output of your program is compared with a correct output file, and if any single byte doesn't match you get WA.

Printing anything extra, like the message "MAX cycle length is " will definitely result in WA, even if numbers are OK.
ithenewguyi
New poster
Posts: 5
Joined: Tue Nov 20, 2007 11:27 pm

Q: java input

Post by ithenewguyi »

can anyone explain how does the java input provided works?
why cant we use Scanner? for some problems, how do we tell if the input is over? do we use Scanner.hasNext() method?
thanks for any "input" :D
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

You can use Scanner.
ithenewguyi
New poster
Posts: 5
Joined: Tue Nov 20, 2007 11:27 pm

Post by ithenewguyi »

thanks for the reply, and so we could use .hasNext() method to check if the input is finished?

I'm not sure why i get a WA then
for the problem 100, i took care of the i>j or i<j and still...

Code: Select all

/* problem 100
 * 3n + 1 problem
 */
import java.util.*;
class Main
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner (System.in);
    try {
      while (in.hasNext()){
        String numbers = in.nextLine();
        String[] num = numbers.split(" ");
        
        long num1 = Long.parseLong(num[0]);
        long num2 = Long.parseLong(num[1]);
        
        long big;
        long small;
        if (num1 > num2){
          big = num1;
          small = num2;
        }
        else {
          big = num2;
          small = num1;
        }
        long temp;
        long counter=1;
        long biggest=1;
        for (long i = small; small <= big; small++)
        {
          temp = small;
          while (temp != 1){
            if (temp % 2 == 0){
              temp /= 2;
              counter++;
            }
            else{
              counter++;
              temp = temp * 3 +1;
            }
          }
          if (counter>biggest)
            biggest=counter;
          counter=1;
        }
        System.out.println(numbers+" "+biggest);
      }
    }catch (Exception e){System.exit(0);}
  }
}
Post Reply

Return to “Volume 1 (100-199)”