Posted: Sun Aug 20, 2006 8:40 pm
Java support is mentioned here. It will be greatly improved in the coming new Judge.
Code: Select all
#include <iostream>
using namespace std;
int main(){
int i=0;
int j=0;
while(cin>>i>>j){
int count;
int val=0;
if(i>j){
int tmp;
tmp=i;
i=j;
j=tmp;
}
for(int s=i;s<=j;s++){
count=1;
int k=s;
while(k!=1){
if(k%2==1) k=3*k+1; //odd
else k=k/2; //even
count++;
}
if(count>=val) val=count;
}
cout<<i<<' '<<j<<' '<<val<<endl;
}
return 0;
}
Code: Select all
#include <iostream>
using namespace std;
int main(){
int i=0;
int j=0;
while(cin>>i>>j){
int count;
int val=0;
if(i>j){
int tmp;
tmp=i;
i=j;
j=tmp;
}
for(int s=i;s<=j;s++){
count=1;
int k=s;
while(k!=1){
if(k%2==1) k=3*k+1; //odd
else k=k/2; //even
count++;
}
if(count>=val) val=count;
}
cout<<i<<' '<<j<<' '<<val<<endl;
}
return 0;
}
Thank you!jtmh wrote:Java support is mentioned here. It will be greatly improved in the coming new Judge.
Note from the output description:law1009 wrote:[situation]
It could run well in Dev C++, but could't pass the demo of acm.
I already tried my best to find the bug but in vain.
Thanks for helping!
Code: Select all
#include <iostream> using namespace std; int main(){ int i=0; int j=0; while(cin>>i>>j){ int count; int val=0; if(i>j){ int tmp; tmp=i; i=j; j=tmp; } for(int s=i;s<=j;s++){ count=1; int k=s; while(k!=1){ if(k%2==1) k=3*k+1; //odd else k=k/2; //even count++; } if(count>=val) val=count; } cout<<i<<' '<<j<<' '<<val<<endl; } return 0; }
The integers i and j must appear in the output in the same order in which they appeared in the input...
I wanted to quote this rather old post, because in a sense it is right. The values do overflow with some of the bigger values allowed for the input in my program and it got AC.59446TY wrote:I have gotten problem 100 to (accepted) in both Java an C, and I believe there to be a problem with the judge. The C program works no problem, but initially the Java program did not. I added a "hack" to the Java program, which actually causes it to miscalculate some values, and this program was accepted as correct.
The problem is that, in C even an unsigned long is not large enough to handle some of the numbers that will be encountered during calculation of the 3n+1 series. These numbers are (incorrectly) truncated, thus making 3n+1 < n sometimes. This doesn't cause any problems, assuming that I write my program in C, and the judge solutions are done in C, as they make the same mistakes and get matching answers.
However, if you use Java, it doesn't have the same limit for its long integers. It correctly calculates different values, thus not matching the (incorrect) judge. If you add in a hack into your java code, that is to truncate any value over the LONG_MAX value of c, you can get the java program to be accepted.
Just thought you might want to know that your "accepted" programs are all flawed, and perhaps it will help some students to be less frustrated with non-working java submissions.
I think that since ints in JAVA are 32-bits then JAVA users should just use unsigned ints, but probably something has to be corrected in the problem's prompt for example an imperative "Assume that no operation overflows a 32-bit integer." or "Use 32 bits integers , assume that no operation overflows a 32-bit integer"You can assume that no operation overflows a 32-bit integer.
Code: Select all
#include <iostream>
using namespace std;
int main(void){
long i,j,m,temp,total,max;
while(cin>>i>>j){
cout<<i<<" "<<j<<" ";
max=0;
if(i>j){
temp=i;
i=j;
j=temp;
}
for(m=i;m<=j;m++){
temp=m;
total=0;
while(1){
if(temp==1){
total++;
break;
}
total++;
if(temp%2==0) temp/=2;
else temp=3*temp+1;
}
if(total>max) max=total;
}
cout<<max<<endl;
}
return 0;
}
Code: Select all
var
i,j,m,temp,total,max:integer;
begin
while not eof(input) do begin
readln(i,j);
write(i,' ',j,' ');
max:=0;
if i>j then begin
temp:=i;
i:=j;
j:=temp;
end;
for m:=i to j do begin
temp:=m;
total:=0;
while true do begin
if temp=1 then begin
inc(total);
break;
end;
inc(total);
if temp mod 2 = 0 then
temp:=temp div 2 else
temp:=3*temp+1;
end;
if total>max then
max:=total;
end;
writeln(max);
end;
end.
Because of INFINITE LOOP:blitzritz wrote:can anyone please tell me why am i getting Time Limit Exceeded??!!![]()