Page 54 of 93
Posted: Wed Jul 26, 2006 6:11 am
by mf
cout<<"enter two number i,j:"; //enter two number as it's range
cout<<"maximum cycle length:"<<max<<endl; //output maximum cycle length
Don't output anything that you aren't explicitely asked to output.
If you give your program the input, given in "Sample Input" section, it must produce exactly the same output as in "Sample Output".
is enter 4 pair of value in same time??
and output the four pair of value in same time??
That's correct.
Check
http://acm.uva.es/p/data/p100.c.html for a sample accepted solution.
Posted: Wed Jul 26, 2006 7:10 am
by toyman
Check
http://acm.uva.es/p/data/p100.c.html for a sample accepted solution.[/quote]
Your code
while (scanf("%d %d\n",&m,&n)==2){
why shoule "scanf("%d %d\n",&m,&n)==2"....
and my code...
Code: Select all
#include<iostream>
using namespace std;
int compute(int i); //define function
main()
{
int h,i,j,max,swap; //define variables
while(cin>>i>>j){
if (i > j){
swap = i;
i = j;
j = swap;
}
max = compute(i);
for(h=i+1;h<j;h++){
swap=compute(h);
if(swap>max){
max=swap;
}
}
cout<<i<<" "<<j<<" "<<max<<endl; //output maximum cycle length
}
}
int compute(int i)
{
int count=1;
while(i!=1){
if(i%2)
i=3*i+1;
else
i=i/2;
count++;
}
return count;
return 0;
}
My ouput is
1 10
1 10 20
100 200
100 200 125
....
--
I have poor ability in coding@@
Posted: Wed Jul 26, 2006 7:20 am
by mf
Your new code is almost correct. But please note this sentence in the problem statement:
The integers i and j must appear in the output in the same order in which they appeared in the input
(Thus, output for input "200 100" should be "200 100 125".)
Posted: Wed Jul 26, 2006 9:11 am
by toyman
Code: Select all
#include<iostream>
using namespace std;
int compute(int i); //define function
main()
{
int max[20],ioo[20],joo[20];//define variables
int swap,x=0,times=0,k;
int h,i=0,j=0;
while((cin>>i>>j)){
ioo[times]=i;
joo[times]=j;
if (i > j){
swap = i;
i = j;
j = swap;
}
max[times] = compute(i);
for(h=i+1;h<j;h++){
swap=compute(h);
if(swap>max[times]){
max[times]=swap;
}
}
times++;
}
//cout<<"The output"<<endl;
for(k=0;k<times;k++){
cout<<ioo[k]<<" "<<joo[k]<<" "<<max[k]<<endl;
}
system("pause");
return 0;
}
int compute(int i)
{
int count=1;
while(i!=1){
if(i%2)
i=3*i+1;
else
i=i/2;
count++;
}
return count;
return 0;
}
Runtime Error
Your program has died with signal 11 (SIGSEGV). Meaning:
Invalid memory reference
Before crash, it ran during 4.162 seconds.
My input is
1 10
10 1
100 200
201 210
22222222222
My output is
1 10 20
10 1 20
100 200 125
201 210 89
Is what my thought match the question's answer ???
This what i want to know?
Maybe i have wrong idea for this question!!
Posted: Wed Jul 26, 2006 9:35 am
by mf
You got runtime error because there are more than 20 test cases in the input.
Your previous code is quite working, as I said. Just do cout<<i<<" "<<j<<" "; before swapping i and j, and change the condition of the for-loop to "h<=j":
Code: Select all
int main()
{
int h,i,j,max,swap;
while(cin>>i>>j){
cout<<i<<" "<<j<<" "; // outputs i and j in their original order
if (i > j){
swap = i;
i = j;
j = swap;
}
max = compute(i);
for(h=i+1;h<=j;h++){ // checks numbers between i+1 and j inclusive
swap=compute(h);
if(swap>max){
max=swap;
}
}
cout<<max<<endl;
}
}
...
Posted: Wed Jul 26, 2006 10:02 am
by toyman
3QQQ~~
My question have solved!!!
Thank your help!!

Posted: Wed Jul 26, 2006 11:57 pm
by diego andres de barros
instead of while (scanf("%ld %ld",&i,&j)!=EOF)
the correct is while (scanf("%ld %ld",&i,&j)==2)
100 in haskell
Posted: Thu Jul 27, 2006 6:54 am
by lck
Why it take so long to calculate the answer in haskell while only one sec in c??
Code: Select all
f 1 = 1
f n = 1 +
if even n
then f $ div n 2
else f $ n * 3 + 1
longCyLn i j = maximum $ map f [i..j]
putStrLn longCyLn 1 4000

WA in 100
Posted: Thu Jul 27, 2006 9:34 am
by Chiech
#include<iostream.h>
void main()
{
unsigned long i,i1,j,j1,cup;
long m;
do
{
cin>>i>>j;
}while(i<=0||i>1000000||j<=0||j>1000000);
i1=i;j1=j;
m=j-i+1;
if(m<=0)
{
m=i-j+1;
cup=i,i=j,j=cup;
}
else
{
cup=i;
}
unsigned long *a1_array=new unsigned long [j*3];
unsigned long *a2_array=new unsigned long [j*3];
unsigned long n,x,y=1;
start:
x=1,n=1;cup=i;
for(x=1;;x++)
{
*(a1_array+x)=cup;
if(cup==1)
{
*(a2_array+y)=n;
y++;
i++;
if(i>j)
{
goto end;
}
goto start;
}
else if(*(a1_array+x)%2==0)
{
n++;
cup=*(a1_array+x)/2;
continue;
}
else
{
cup=3*cup+1;
n++;
continue;
}
}
end:
unsigned long z;
*(a2_array)=0;
for(z=1;z<y;z++)
{
if(*(a2_array)<*(a2_array+z)){*(a2_array)=*(a2_array+z);}
}
cout<<i1<<" "<<j1<<" "<<*(a2_array)<<"\n";
delete[] a1_array;
delete[] a2_array;
}
This is my code~please help me ~I get wa~"~
Posted: Thu Jul 27, 2006 10:51 am
by jtmh
diego andres de barros wrote:instead of while (scanf("%ld %ld",&i,&j)!=EOF)
the correct is while (scanf("%ld %ld",&i,&j)==2)
Both work, actually.
why time limit exeeded![100]
Posted: Thu Jul 27, 2006 12:05 pm
by newton
Posted: Thu Jul 27, 2006 6:06 pm
by 898989
Salamo Aleko
One of the ways to get ac is to check on range on fly
make a function that get a cycle of a number
Then in main() procees directly on the range.....Then try to solve 371
Posted: Fri Jul 28, 2006 4:29 pm
by alternate
scanf returns an integer containing the number of items read. EOF is returned if no items can be read therefore in this case it's better to compare to the number of items you require in case only one of two are input. Although, you could argue that you know the input is valid in this case so comparing to EOF will also work.
Posted: Fri Jul 28, 2006 4:47 pm
by alternate
Loop between i and j for each input and update max to n when n is greater than max.
Posted: Fri Jul 28, 2006 8:35 pm
by jtmh
I agree.
