Skip to content

백준 2217 로프 문제 질문 #3

Description

@cozups

문제를 풀긴 했는데... 제가 실패했던 코드의 문제점을 알고싶습니다!!! 발견하시는분 코멘트 부탁드립니다..

  • 실패한 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] rope = new int[n];

        for(int i=0; i<n; i++){
            rope[i] = Integer.parseInt(br.readLine());
        }

        Arrays.sort(rope);

        int ans = 0;
        for(int i=0; i<n; i++, n--){
            ans = Math.max(ans, rope[i] * n);
        }

        System.out.println(ans);
    }
}
  • 성공한 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] rope = new int[n];

        for(int i=0; i<n; i++){
            rope[i] = Integer.parseInt(br.readLine());
        }

        Arrays.sort(rope);

        int ans = 0; int cnt = 1;
        for(int i=n-1; i>=0; i--){
            ans = Math.max(ans, rope[i] * cnt);
            cnt++;
        }

        System.out.println(ans);
    }
}

두 코드의 차이점은 마지막에 정답을 구할 때 밧줄 무게가 작은 순서로 구했는지 큰 순서로 구했는지의 차이입니다.

작은 무게의 밧줄부터 했을 땐 틀렸다고 했고 큰 무게의 밧줄부터 했을 땐 맞았다는데....

방법은 같은 것 아닌가요?ㅠㅠ

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions