UVA 3n+1 Problems

5 views
Skip to first unread message

聰┼聰

unread,
Jul 1, 2011, 3:25:26 AM7/1/11
to ACM-CPC-MUST
import java.io.BufferedInputStream;
import java.util.Scanner;

class Main {
public static void main(String args[]){
long count;
long max = 0;
long n = 0;
int from = 0, tmpFrom = 0;
int to = 0, tmpto = 0;
Scanner br = new Scanner(new BufferedInputStream(System.in));
while(br.hasNext()){
tmpFrom = from = br.nextInt();
tmpto = to = br.nextInt();

if(from>to){
to = tmpFrom;
from = tmpto;
}
max = 0;
for(long i=from; i<=to; i++){
count = 1;
n = i;
while(n != 1){
if((n & 1) == 1){
n = (n << 2) - n +1;
}else{
n = n >> 1;
}
count++;
}
if(count > max){
max = count;
}
}
System.out.println(tmpFrom + " " + tmpto + " " + max);
}
}
}

Henry Hong-Ning Dai

unread,
Jul 1, 2011, 10:59:36 AM7/1/11
to acm-cp...@googlegroups.com
Dear Jeff,

Have your code passed the UVA online judge?

Regards,
Henry
--
Hong-Ning Dai, Ph.D.
Assistant Professor
Faculty of Information Technology
Macau University of Science and Technology
Tel: (853) 8897 2154
Email: hndai...@gmail.com

聰┼聰

unread,
Jul 1, 2011, 11:02:08 AM7/1/11
to ACM-CPC-MUST
Yes,I have. but the running time around 1S

> Email: hndai.m...@gmail.com

Henry Hong-Ning Dai

unread,
Jul 1, 2011, 11:08:19 AM7/1/11
to acm-cp...@googlegroups.com
I think it is acceptable for Java. Another student tried this by C++ and his runtime is about 0.6s.

Go ahead for other questions. Good luck!

Henry
Email: hndai...@gmail.com
Reply all
Reply to author
Forward
0 new messages