Page 5 of 93
Posted: Sun Jun 09, 2002 11:48 pm
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]
Posted: Mon Jun 10, 2002 12:20 am
by littledump
not sure as of right now but...
did u try all possible cases to ensure the output is correct?
Posted: Mon Jun 10, 2002 12:27 am
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){
...
Posted: Mon Jun 10, 2002 1:27 am
by Dragoon
I FINALLY found out what was wrong

... 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]
Posted: Mon Jun 10, 2002 4:06 am
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
In prob 100 will the program take all the input and then giv
Posted: Mon Jun 10, 2002 12:14 pm
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.
Posted: Mon Jun 10, 2002 12:29 pm
by Stefan Pochmann
Why does this matter? Btw, it's *your* program and you want *us* to tell you what it does?
Re: In prob 100 will the program take all the input and then
Posted: Mon Jun 10, 2002 3:30 pm
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
Please tell me where is the problem
Posted: Tue Jun 11, 2002 6:58 am
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.
Run Time ERROR Prob No. 100
Posted: Tue Jun 11, 2002 9:47 am
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
Posted: Tue Jun 11, 2002 3:25 pm
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.
Posted: Tue Jun 11, 2002 3:53 pm
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 !!!
Posted: Tue Jun 11, 2002 4:01 pm
by Betovsky
hint !! hint !!
scanf returns an int...
Posted: Tue Jun 11, 2002 6:08 pm
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
Posted: Tue Jun 11, 2002 6:16 pm
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...