반응형

백준 16431번 베시와 데이지 풀이 코드

C | C++ | Java | Python


풀이

베시는 대각선으로 이동할 수 있으므로 가로 세로 거리 중 더 긴 거리가 곧 이동 횟수입니다. 데이지는 대각선으로 이동할 수 없으므로 반드시 가로 세로 거리를 모두 거쳐야 하고, 결국 두 거리를 합친 것이 곧 이동 횟수입니다. 출력 결과는 간단한 조건문으로 나눌 수 있습니다.

max, abs 함수를 사용하면 더 편하게 코드를 짤 수 있습니다. C언어에는 max함수가 없으므로 매크로로 지정해서 사용합니다.

코드

#include <stdio.h>
#include <stdlib.h>
#define max(x, y) ((x) > (y) ? (x) : (y))

int main(){
    int Bx, By, Dx, Dy, Jx, Jy, B, D;
    scanf("%d %d", &Bx, &By);
    scanf("%d %d", &Dx, &Dy);
    scanf("%d %d", &Jx, &Jy);
    B = max(abs(Jx-Bx), abs(Jy-By));
    D = abs(Jx-Dx) + abs(Jy-Dy);
    if (B == D)
        printf("tie");
    else if (B < D)
        printf("bessie");
    else
        printf("daisy");
    return 0;
}
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main(){
    int Bx, By, Dx, Dy, Jx, Jy, B, D;
    cin>>Bx>>By>>Dx>>Dy>>Jx>>Jy;
    B = max(abs(Jx-Bx), abs(Jy-By));
    D = abs(Jx-Dx) + abs(Jy-Dy);
    if (B == D)
        cout<<"tie";
    else if (B < D)
        cout<<"bessie";
    else
        cout<<"daisy";
    return 0;
}
import java.util.Scanner;
import java.lang.Math;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int Bx = sc.nextInt(), By = sc.nextInt();
        int Dx = sc.nextInt(), Dy = sc.nextInt();
        int Jx = sc.nextInt(), Jy = sc.nextInt();
        int B = Math.max(Math.abs(Jx-Bx), Math.abs(Jy-By));
        int D = Math.abs(Jx-Dx) + Math.abs(Jy-Dy);
        if (B == D)
            System.out.println("tie");
        else if (B < D)
            System.out.println("bessie");
        else
            System.out.println("daisy");
    }
}
Bx, By = map(int, input().split())
Dx, Dy = map(int, input().split())
Jx, Jy = map(int, input().split())
B = max(abs(Jx-Bx), abs(Jy-By))
D = abs(Jx-Dx) + abs(Jy-Dy)
if B == D:
    print("tie")
elif B < D:
    print("bessie")
else:
    print("daisy")

문제 출처

https://www.acmicpc.net/problem/16431

반응형

+ Recent posts