thd0011   6년 전

50% 까지 갔는데 갑자기 틀렸습니다 하고 나오는데 왜이런걸까요... 

import java.util.Scanner;
public class Main {
    static int x[] = new int[1000000];
    static final int MUL = 1000000;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int m = Integer.parseInt(scan.nextLine());
        double l = MUL / (double) m;
        int scope[] = new int[m];
        for (int i = 0; i < m; i++) {
            scope[i] = (i + 1) * (int) l;
        }
        String arr[] = scan.nextLine().split(" ");
        for (int i = 0; i < arr.length; i++) {
            x[i] = (int) (MUL * Double.parseDouble(arr[i]));
        }
        int count[] = new int[m];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < m; j++) {
                if (x[i] < scope[j]) {
                    count[j]++;
                    break;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            System.out.print(count[i] + " ");
        }
    }
}

chogahui05   6년 전

실수 오차 때문에 틀린 거네요. ㅎㅎ

이런 류의 문제가 사실 골때리는데요. 정확한 풀이는 double형으로 푸는 게 아니고요.

정수형으로 변환해서 푸시는 게 좋아보입니다.

chogahui05   6년 전

그나저나 하나 궁금한 게요.


double l = MUL / (double) m;

얘는 맞아보여요. 그런데 왜


for (int i = 0; i < m; i++) {
            scope[i] = (i + 1) * (int) l;
        }

이래 코딩하셨나요? scope가 정수로 떨어진다는 보장이 있나요?

thd0011   6년 전

데이터들도 10^6을 곱해서 정수로 변환을 해서 scope도 그냥 정수로 변환했어요 ㅎㅎㅎ...

로그찍어보니깐 double오차 범위때문에 정확한 값이 안들어오네요...

Math.ceil로 그냥 올림처리해서 제출하니깐 일단 맞기는 맞았네여;; ㅋㅋㅋ

댓글을 작성하려면 로그인해야 합니다.