May 22, 2013 at 10:02am May 22, 2013 at 10:02am UTC
i am a beginner . i am trying to submit a solution of 3n+1 problem.it works properly but when i submit this , wrong answer is replied.please help me to solve this .
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
long int start,end,temp=0;
long int value[1000000];
string startvalue,endvalue;
while(cin>>start>>end){
//cin>>start>>end;
//getline(cin,startvalue,endvalue);
//stringstream(startvalue) >> start;
//getline(cin,endvalue);
//stringstream(endvalue) >> end;
//cout<<"\t";
for(int i=start;i<=end;i++){
long int j=i;
value[i]=1;
if(j==1){
temp=value[i];
continue;
}
else{
while(j>1){
if ((j%2)!=0){
j=3*j+1;
//end of if ((i%2)!=0)
}
else
{j=j/2;}
value[i]++;
}
}
if(i>start&&value[i]>temp){temp=value[i];}
if(i==start){temp=value[i];}
}
cout<<start<<" "<<end<<" "<<temp<<"\n";
}
return 0;
//end of main loop
}
May 22, 2013 at 10:29am May 22, 2013 at 10:29am UTC
a) Please use code tags. It is "<>" button
b) What 3n+1 problem is?
Probably you have problems with output format. Remember: any missing or excessive whitespace or newline can lead to rejection.
May 22, 2013 at 10:43am May 22, 2013 at 10:43am UTC
long int value[1000000];
is too big, it may cause a stack overflow.
Resize it to a decent size, like 1000, 2000 or so.
May 22, 2013 at 12:06pm May 22, 2013 at 12:06pm UTC
while (cin>>start>>end)
debería ser
1 2 3
cout <<"enter star and end" ;
cin>>start;
cin>>end;
what is in parenthesis is a expression
while (start <end)
start is less than end
while (start > end)
start is greater than end
http://www.cplusplus.com/doc/tutorial/control/
for (int i=start;i<=end;i++)
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
int i is not equal to start.
stack overflow
http://www.cplusplus.com/doc/tutorial/variables/
Last edited on May 22, 2013 at 12:39pm May 22, 2013 at 12:39pm UTC
May 22, 2013 at 12:37pm May 22, 2013 at 12:37pm UTC
@flony1
This is a correct and safe way to read values until the end of stream.
He did everything right here.
May 25, 2013 at 6:14am May 25, 2013 at 6:14am UTC
i don't know if you're about going through the CSUS, but in all cases execution time is really important in such problem, i happened to have a look at the problem -really challenging it is-, your algorithm to statically allocate 1million variables in try to store all the sequence is really capable of frying lots of computers.
i suggest looking for another algorithm ASAP.
May 25, 2013 at 7:53am May 25, 2013 at 7:53am UTC
> What 3n+1 problem is?
Details.
long int
is may be too short
Last edited on May 25, 2013 at 7:57am May 25, 2013 at 7:57am UTC