cjswo8219   2년 전

어디에서 오류가 생기는지 모르겠어요ㅠㅠ

모든 경우의수에서 다 작성했는데...어디서 틀리는건가요?


jungwooking   2년 전

preview

cjswo82 님의 코드에 2가지 문제점이 있는 것 같습니다

if(a ==0){
    if(b <= 0) {
        System.out.println("1");
    } else if((double) b/c <= d) {
        System.out.println("0");
    }
} 에서 위에 그림에서 보시다시피 d가 두 직선의 교점인 b/c보다 크다면 b가 양수냐 음수냐에 상관없이 d 이후에 나오는 x에서 무조건 cx가 ax+b보다 큽니다.


따라서

if(a ==0){
    if((double) b/c <= d) {
        System.out.println("1");
    } else {
   System.out.println("0");
 }
} 로 수정해주시면 되겠습니다

다음은 

if(a > 0) {
if(a < c && (double) b / (c-a) <= d) {
System.out.println("1");
} else if(a > c && d <= (double) b / (c-a)) {
System.out.println("1");
} else {
System.out.println("0");
}
} 에서 첫번째 a < c일 때는 잘하셨는데 a와 c가 같을 때도 (double) b / (c-a) <= d 라면 cx >= ax+b가 가능하기 때문에 a <= c 로 수정해주셔야할 것 같고

두번째 조건인 a >c에서는 위의 그림에서 보시다시피 정의를 잘 생각해보시면 d보다 큰 모든 x에 대해 cx >= ax+b 여야 하는데 

cx와 ax+b의 교점보다 d가 작으면 교점 전까지는 cx >= ax+b를 만족하지만 교점 이후에는 cx < ax+b가 되어버리므로 결국 a가 c보다 기울기가 크다면 절대로 O(cn)=ax+b를 만족할 수 없습니다 (기울기가 크다면 교점 이후에 x에서는 무조건 ax+b > cx이기 때문에)


if(a > 0) {
if(a <= c && (double) b / (c-a) <= d) {
System.out.println("1");
} else {
System.out.println("0");
}
} 로 수정하시면 되겠습니다

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main {

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(br.readLine());
int d = Integer.parseInt(br.readLine());


if(a < 0) {
if((double) b / (c-a) <= d) {
System.out.println("1");
} else {
System.out.println("0");
}
}

if(a ==0){
if((double) b/c <= d) {
System.out.println("1");
} else {
System.out.println("0");
}
}
if(a > 0) {
if(a <= c && (double) b / (c-a) <= d) {
System.out.println("1");
} else {
System.out.println("0");
}
}


}
}

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