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

Post Reply
Dragoon
New poster
Posts: 7
Joined: Sun Jun 09, 2002 5:51 am
Location: BC, Canada
Contact:

Post by Dragoon »

Alrighty, I added
[cpp]while(cin >> start >> end)
{
}[/cpp]
to my code and judge decided
Dear Dragoon:

Your program has not solved the problem. It ran during 0.040 seconds.
Does anyone see what im doing wrong?
Here is my code:
[cpp]//judge@uva.es
//@JUDGE_ID: 20308JC 100 C++
//@BEGIN_OF_SOURCE_CODE

#include <iostream.h>

int maxcycle(int n)
{
int count = 1;
while(n != 1)
{
if(n % 2)
n = 3 * n + 1;
else
n = n / 2;
count++;
}
return count;
}

int main()
{
int start = 0;
int end = 0;
int temp = 0;
int largest = 0;

// cout << "Please enter 2 integers separated by a space or a new line:\n";

while(cin >> start >> end)
{
if(start > 0 && start < 10000 && end > 0 && end < 10000)
{
cout << start << " " << end << " ";

if(start < end)
{
while(start <= end)
{
temp = maxcycle(start);
if(temp > largest)
largest = temp;
start++;
}
}
if(start > end)
{
while(start >= end)
{
temp = maxcycle(end);
if(temp > largest)
largest = temp;
end++;
}
}

cout << largest << endl;
}
}
// else
// cout << "Integers must be between 0 and 10000.\n";
// main();
return 0;
}
//@END_OF_SOURCE_CODE[/cpp]
littledump
New poster
Posts: 25
Joined: Wed Jun 05, 2002 4:55 am
Location: London, ON, Canada
Contact:

Post by littledump »

not sure as of right now but...

did u try all possible cases to ensure the output is correct?
Betovsky
New poster
Posts: 26
Joined: Wed Jun 05, 2002 7:30 pm
Location: Portugal

Post by Betovsky »

u have:

if(start < end){
while(start <= end){
...
start++;

so when get out of the cicle (start == end+1) ->> start > end

so it will get into the next if:

if(start > end){
while(start>=end){
...
Dragoon
New poster
Posts: 7
Joined: Sun Jun 09, 2002 5:51 am
Location: BC, Canada
Contact:

Post by Dragoon »

I FINALLY found out what was wrong :D... I fixed what Betovsky said, and i found out that the "largest" variable needed to be reset to 0 each time i get a new one to calculate...

Now i'm wondering why it took so long... "0.030 seconds"

Is there anywhere to keep track of the problems that ive solved?

Here is my code:
[cpp]//judge@uva.es
//@JUDGE_ID: 20308JC 100 C++
//@BEGIN_OF_SOURCE_CODE

#include <iostream.h>

int maxcycle(int n)
{
int count = 1;
while(n != 1)
{
if(n % 2)
n = 3 * n + 1;
else
n = n / 2;
count++;
}
return count;
}

int main()
{
int start = 0;
int end = 0;
int temp = 0;
int largest = 0;

// cout << "Please enter 2 integers separated by a space or a new line:\n";

while(cin >> start >> end)
{
largest = 0;
if(start > 0 && start < 10000 && end > 0 && end < 10000)
{
cout << start << " " << end << " ";

if(start <= end)
{
while(start <= end)
{
temp = maxcycle(start);
if(temp > largest)
largest = temp;
start++;
}
}
else if(start > end)
{
while(start >= end)
{
temp = maxcycle(end);
if(temp > largest)
largest = temp;
end++;
}
}

cout << largest << endl;
}
}
// else
// cout << "Integers must be between 0 and 10000.\n";
// main();
return 0;
}
//@END_OF_SOURCE_CODE[/cpp]
Betovsky
New poster
Posts: 26
Joined: Wed Jun 05, 2002 7:30 pm
Location: Portugal

Post by Betovsky »

at http://acm.uva.es/problemset/ goto the part of information in real time
go for the statics for a single author and put ur id
taj79
Learning poster
Posts: 74
Joined: Sun Jun 09, 2002 11:56 am
Location: India
Contact:

In prob 100 will the program take all the input and then giv

Post by taj79 »

My question is that in prob No 100

will the program take all the input lines and then give output
OR
with each line of input it will give the output?

If its the 1st case then how to read the input.Please tell me.Because in other problems (next 2 )also I have same doubt.
Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

Why does this matter? Btw, it's *your* program and you want *us* to tell you what it does?
skylander
New poster
Posts: 13
Joined: Mon Jun 10, 2002 3:24 pm
Contact:

Re: In prob 100 will the program take all the input and then

Post by skylander »

taj79 wrote:My question is that in prob No 100

will the program take all the input lines and then give output
OR
with each line of input it will give the output?

If its the 1st case then how to read the input.Please tell me.Because in other problems (next 2 )also I have same doubt.
it will work either way, i think the judge look at the final output
taj79
Learning poster
Posts: 74
Joined: Sun Jun 09, 2002 11:56 am
Location: India
Contact:

Please tell me where is the problem

Post by taj79 »

I have modified my solution:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int maxlenij(int i,int j);/* gives maximum cycle length for integers between and including i and j */
int len(int u);


main()
{ int t,i,j,len;
int m=0,n=0;
char u[20],temp1[20],temp2[20];





for(t=1;;++t)/* t = no. of pairs */
{
gets(u);
u[strlen(u)+1]='\0';
if( !strcmp(u,"\0")){ exit(1); }



m=0; n=0;
while(u[m]!= ' '){ temp1[m]=u[m]; m++; }

temp1[m]='\0';m++;

/* printf("%s %d %d %d\n",temp1,strlen(temp1),m,strlen(u)); */

while(m<=strlen(u)){ temp2[n]=u[m]; m++;n++; }
temp2[n]='\0';

/* printf("%s %d\n",temp2,strlen(temp2)); */

i=atoi(temp1);
j=atoi(temp2);



printf(" %3d %3d %3d\n",i,j,maxlenij(i,j) );

}


}


Rest is same.
The judge gives the same error.

Could you please tell me how to correct it.

I feel the problem is while taking the input.But I really can't see any problem when I run the program in my computer.
taj79
Learning poster
Posts: 74
Joined: Sun Jun 09, 2002 11:56 am
Location: India
Contact:

Run Time ERROR Prob No. 100

Post by taj79 »

I have given main part of my prog here.Please read it and then help me in the questions I have asked after that.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 1000
int maxlenij(int i,int j);/* gives maximum cycle
length for integers between and including i and j */
int len(int u);


main()
{ int t,i[MAX],j[MAX],len;
for(t=1;;t++)
{
scanf("%d%d",&i[t],&j[t]);
if(t>1 && i[t] == i[t-1] && j[t] == j[t-1] )
exit(1);
printf(" %3d %3d
%3d\n",i[t],j[t],maxlenij(i[t],j[t]) );
}/* END of FOR */

}

I made a file named " test" like this
1 10
100 200
200 201

Then I ran my prog in linux like this
./prog < test

In the for loop why I made this code is that:

First I had made it like this:
for(t=1;;t++)
{
scanf("%d%d",&i,&j);
printf(" %3d %3d
%3d\n",i,j,maxlenij( i, j) );
}

while running this I found that after the prog couldn't find any more integer it went on and on in the loop with the last set of i and j.

So what I am trying to do with my new code is that as it reaches the last line of input and the next set of i and j takes the same value as earlier one the loop exits.
But with this new code I am getting RUN TIME ERROR given by judge.
Please tell me how to deal with this.I am sure the prob with my prog is it doesn't know to stop after the last input line.
Kindly help me out.I am new to ACM.The next few problems are also taking the input like this prog.If I can't do this I won't be able to do the next few problems .
If possible send me some code(if not in public ,sent me in private message)

Thanks
taj
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

COULD you please post your question under the thread about your problem.

To use this discussion board, I think it's better to obey the rule, and don't just for your convenience.
Betovsky
New poster
Posts: 26
Joined: Wed Jun 05, 2002 7:30 pm
Location: Portugal

Post by Betovsky »

hmm.. first i think u cannot use gets in ansi C
at least to me it gives some warnings
of dangerous function

and why dont u use scanf it would be a lot more easy !!!
Betovsky
New poster
Posts: 26
Joined: Wed Jun 05, 2002 7:30 pm
Location: Portugal

Post by Betovsky »

hint !! hint !!

scanf returns an int...
taj79
Learning poster
Posts: 74
Joined: Sun Jun 09, 2002 11:56 am
Location: India
Contact:

Post by taj79 »

I have given main part of my prog here.Please read it and then help me in the questions I have asked after that. I have MODIFIED it again.But this it gave RUN TIME ERROR.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 1000
int maxlenij(int i,int j);/* gives maximum cycle
length for integers between and including i and j */
int len(int u);


main()
{ int t,i[MAX],j[MAX],len;
for(t=1;;t++)
{
scanf("%d%d",&i[t],&j[t]);
if(t>1 && i[t] == i[t-1] && j[t] == j[t-1] )
exit(1);
printf(" %3d %3d
%3d\n",i[t],j[t],maxlenij(i[t],j[t]) );
}/* END of FOR */

}

I made a file named " test" like this
1 10
100 200
200 201

Then I ran my prog in linux like this
./prog < test

In the for loop why I made this code is that:

First I had made it like this:
for(t=1;;t++)
{
scanf("%d%d",&i,&j);
printf(" %3d %3d
%3d\n",i,j,maxlenij( i, j) );
}

while running this I found that after the prog couldn't find any more integer it went on and on in the loop with the last set of i and j.

So what I am trying to do with my new code is that as it reaches the last line of input and the next set of i and j takes the same value as earlier one the loop exits.
But with this new code I am getting RUN TIME ERROR given by judge.
Please tell me how to deal with this.I am sure the prob with my prog is it doesn't know to stop after the last input line.
Kindly help me out.I am new to ACM.The next few problems are also taking the input like this prog.If I can't do this I won't be able to do the next few problems .
If possible send me some code(if not in public ,sent me in private message)

Thanks
taj
Betovsky
New poster
Posts: 26
Joined: Wed Jun 05, 2002 7:30 pm
Location: Portugal

Post by Betovsky »

hehe u complicate soo much...

u dont need the arrays
and to see if u reached the end use the return value of scanf
if it returns 0 its cause theres no more to do...
Post Reply

Return to “Volume 1 (100-199)”