All about problems in Volume I. If there is a thread about your problem, please use it. If not, create one with its number in the subject.
Moderator: Board moderators
by DzMx » Sat Feb 20, 2010 4:02 am
Heres my code hoping any help, it throws me a RTE i have removed leading and trailing spaces but it still throws me the error, test inputs (the ones from the problemset ) are showing correct solution, so if you have another inputs they will be helpful too. thx
- Code: Select all
public class Main {
static long count;
static long max;
static long[] array;
public static void main(String args[]) throws java.io.IOException{
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
java.io.PrintWriter out = new java.io.PrintWriter(System.out);
array= new long[10000001];
String line;
while ((line=reader.readLine())!=null) {
if (line == null) {
// end of file
break;
} else if (line.length() == 0) {
break;
} else {
line=line.trim();
String[] entradas = line.split(" ");
long firstO = Long.valueOf(entradas[0]);
long secondO = Long.valueOf(entradas[entradas.length-1]);
long first=firstO;
long second=secondO;
if (first > second) {
long temp = second;
second = first;
first = temp;
}
for(long i=second;i>=first;i--){
count=calculaNumeros(first,i);
if(count>max){
max=count;
}
}
out.print(firstO+" ");
out.print(secondO+" ");
out.println(max);
max=0;
count=0;
}
}
out.flush();
}
public static long calculaNumeros(Long i, Long j) {
if(array[j.intValue()]>0){
return array[j.intValue()];
}else{
if(j==1){
return 1;
}
if(j%2==0){
long m=1+calculaNumeros(i,j/2);
array[j.intValue()]=m;
return m;
}else{
long m=1+calculaNumeros(i,(j*3)+1);
array[j.intValue()]=m;
return m;
}
}
}
}
-
DzMx
- New poster
-
- Posts: 2
- Joined: Sat Feb 20, 2010 3:27 am
Return to Volume I
Who is online
Users browsing this forum: No registered users and 0 guests