UVA Online Judge 3n+1 problem

I tried many times but i keep getting "wrong answer" ...please tell me what is it wrong...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<fstream>
using namespace std;
ifstream in("data.in");
ofstream out("data.out");
int clen(unsigned long n)
{
    if(n==1)
        return 1;
    else if(!n)
        return 0;

    if(!(n%2))
        return clen(n/2)+1;
    else
        return clen(3*n+1)+1;

}
int main()
{
    unsigned long i,j,k,temp,max=0;
    bool ok=1;
    while(in>>i&&in>>j)
    {
        if(i>j)
        {
            temp=i;
            i=j;
            j=temp;
            ok=0;
        }
        max=0;
        for(k=i;k<=j;k++)
            if(max<clen(k))
            {
                max=clen(k);
            }
        if(ok)    
            out<<i<<' '<<j<<' '<<max<<endl;
        else out<<j<<' '<<i<<' '<<max<<endl;
    }
    return 0;
}

http://uva.onlinejudge.org/external/1/100.pdf
Last edited on
Well just ran your code and the output is correct. You sure you have "data.in" file in the correct directory and it is named correctly?
yeah...it works perfectly for me too but despite that, it keeps getting rejected and it's becoming annoying
Last edited on
I don't understand what you mean:
it keeps getting rejected

You are not resetting the 'ok' variable.
why are you using files?

Edit: you deserve to get time limit exceed.
Edit2: limit 10000? this says 1000000 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=36
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<fstream>
using namespace std;
unsigned long clen(unsigned long n)
{
	if(n==1)
		return 1;
	if(!(n%2))
		return clen(n/2)+1;
	else 
		return clen(3*n+1)+1;
	
}
int main()
{
	unsigned long i,j,k,temp,max=0;
	while(cin>>i>>j)
	{
		if(i>j)
		{
			temp=i;
			i=j;
			j=temp;
		}
		max=0; 
		for(k=i;k<=j;k++)
			if(max<clen(k))
			{
				max=clen(k);
			}	
			cout<<i<<' '<<j<<' '<<max<<endl;
	}
	return 0;
}
		

@ne555 yeah now i found that i shouldn't be using files but i am still getting "wrong answer"
Also i fixed the 'ok' part :)
Last edited on
Also i fixed the 'ok' part :)
? nope.
Example input
1 10
10 1
Example output
1 10 20
10 1 20

Your output
1 10 20
1 10 20
lol wtf i'm dumb
the problem was in fact that i was not resetting the 'ok' as you have pointed out earlier
it finally got accepted :)
ty a lot
Last edited on
Does your output have an extra line at the end of it?
My output seems right, every time i submit it i get a wrong answer.
could someone help me out here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
       
int main(){
    unsigned long i; 
    unsigned long j;
    bool k = true;
    cin >> i >> j;
    do{
       unsigned long a = i;
       unsigned long b = j;
       if( i > j)
           swap(i,j);
       int max = 1;
       int currmax = max;
       cout << a;
       for(;i <= j; i++){
       for(int x = i; x > 1 ;currmax++){
         if(x%2 == 1)    
           x = 3*x + 1;
         else
           x = x/2;

       }
       if(currmax > max)
       max = currmax;
       currmax = 1;
       }
       
       cout<< " "<< b << " "<< max;
       if(cin >> i >> j){
       cout << endl;

       }
       else
       k = false;
       }
while(k);
       
return 0;
}

int swap(int i, int j){
         
             unsigned long temp = i;
             i = j;
             j = temp;
             
}
Your swap does not work. However it seems that your program use std::swap (that somehow gets included)
Does your output have an extra line at the end of it?
yes, it ends with an endl. The judge should tell "presentation error" instead of "wrong answer", but try it.

Also check out the limits of the problem
Example input:
1 1000000
Example output:
1 1000000 525

Your output:
1 1000000 476

Please indent your code
Topic archived. No new replies allowed.