Topcoder + Java
Me he puesto por fin a aprender Java. Asi que con un buen libro y con ejemplos como topcoder que mas quieres.
Despues de cansarme ya del libro y ver lo basico he decidido ir a por un algoritmo sencillo de topcoder.
PROBLEM STATEMENT
Proposals arrive at our office in a sequence. We give each one a score. We then
calculate its "rank" based on comparison with the scores of the most recent submissions.
Its rank is 1 + the number of recent scores that are greater than it. So a rank of 1
is the best possible rank and indicates that no recent score was greater. A rank of 2
would mean that exactly one recent score was greater.
We need software that can calculate the rank of each score.
Create a class OnLineRank that contains a method calcRanks that is given
k, the number of recent scores to use in each rank calculation, and
int[] scores, the sequence of scores of the arriving proposals. The
method should calculate the rank for each score and return the sum of
all the ranks.
Each rank should be calculated based on the preceding k scores. If fewer
than k scores have preceded this score, base the calculation on all the preceding
scores. (The first proposal is thus guaranteed a rank of 1.)
El problema es sencillo. Recibes un vector con numero y otro numero que te dice la cantidad de scores recientes. Tienes que calcular individualmente en cada elemento del vector e ir acumulando el resultado. Facil para empezar.
import java.util.*;
public class OnLineRank {
public int calcRanks(int k, int[] scores) {
int rankT = 0;
for(int i = 0; i < scores.length; i++){
int rank = 1;
for(int j = Math.max(0,i-k); j < i; j++){
if(scores[j] > scores[i]) rank++;
}
rankT += rank;
}
return rankT;
}
}


En http://www.canaloposiciones.com puedes informarte de las últimas convocatorias de oposiciones y mucho más, temarios, cursos, etc…
Comment by koko — December 15, 2008 @ 1:28 pm