반응형

백준 15726번 이칙연산 풀이 코드

C | C++ | Java | Python


풀이

곱할 때 int 형을 벗어날 수 있으므로 변수는 long long으로, 나눌 때 소수점 아래 수가 나올 수 있으므로 피제수를 double로 맞추고 계산합니다.

곱할 때 int 형을 벗어날 수 있으므로 변수는 long long으로, 나눌 때 소수점 아래 수가 나올 수 있으므로 피제수를 double로 맞추고 계산합니다.

출력은 int지만 나눌 땐 소수점 아래 수가 나올 수 있으므로 변수를 double로 맞추고 계산합니다.

소수점 아래는 버리므로 출력 시 변수를 int로 맞춥니다.

코드

#include <stdio.h>
int main(){
    long long a, b, c, x, y;
    scanf("%lld %lld %lld", &a, &b, &c);
    x = (double)(a * b) / c;
    y = ((double)a / b) * c;
    if (x > y)
        printf("%lld", x);
    else
        printf("%lld", y);
    return 0;
}
#include <iostream>
using namespace std;
int main(){
    long long a, b, c, x, y;
    cin>>a>>b>>c;
    x = (double)(a * b) / c;
    y = ((double)a / b) * c;
    if (x > y)
        cout<<x;
    else
        cout<<y;
    return 0;
}
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
        int x = (int)((a * b) / c);
        int y = (int)((a / b) * c);
        if (x > y)
            System.out.println(x);
        else
            System.out.println(y);
    }
}
a, b, c = map(int, input().split())
x = (a * b) / c
y = (a / b) * c
if x > y:
    print(int(x))
else:
    print(int(y))

문제 출처

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

반응형

+ Recent posts