PostAddsense


Swimmers Topcoder



public class Swimmers
{
    public int[] getSwimTimes(int[] distances, int[] speeds, int current) {
        int[] res = new int[distances.length];
        for (int i=0; i < distances.length; i++) {
            if(distances[i] == 0) res[i] = 0;
            else if(speeds[i] <= current) res[i] = -1;
            else res[i] = (int)((double)distances[i]/(speeds[i]+current) + (double)distances[i]/(speeds[i]-current));
        }
        
        return res;
    }
}


Explanation

distances변수 바로 앞에 double형을 선언해야 정확한 계산이 가능하다.


References