100 - The 3n + 1 problem
Moderator: Board moderators
Need Help, I'm New..
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...
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...
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
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.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
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...
Those are the same inputs and outputs as in the problem.. Help?
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$
Last edited by Hatty on Thu Nov 08, 2007 7:13 am, edited 1 time in total.
To sumon
Just change this line:
Thanks
Keep posting
Sapnil
Just change this line:
Code: Select all
while(n!=1)
to
while(n>1)
Keep posting
Sapnil
"Dream Is The Key To Success"
@@@ Jony @@@
@@@ Jony @@@
To Hatty
Your problem is in taking input.
Make it simple
>> use long data type
>> remove your code after Acc
Thanks
Keep postingh
sapnil
Your problem is in taking input.
Make it simple
Code: Select all
while(scanf("%ld %ld",&j,&i)==2)
{
}
>> remove your code after Acc
Thanks
Keep postingh
sapnil
"Dream Is The Key To Success"
@@@ Jony @@@
@@@ Jony @@@
-
- New poster
- Posts: 1
- Joined: Sun Nov 18, 2007 4:20 pm
about ACM 100 WA
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;
}
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;
}
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.
Printing anything extra, like the message "MAX cycle length is " will definitely result in WA, even if numbers are OK.
-
- New poster
- Posts: 5
- Joined: Tue Nov 20, 2007 11:27 pm
Q: java input
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"
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"

-
- New poster
- Posts: 5
- Joined: Tue Nov 20, 2007 11:27 pm
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...
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);}
}
}