Baekjoonアルゴリズム | 1712番問題: 損益分岐点

出典

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

問題

Baekjoon Online Judgeの1712番問題、損益分岐点を解きます。

入力

正確な入力形式と制約は元の問題文に従います。

出力

問題で求められる答えを出力します。

サンプル入力 1

1000 70 170

サンプル出力 1

11

サンプル入力 2

3 2 1

サンプル出力 2

-1

サンプル入力 3

2100000000 9 10

サンプル出力 3

2100000001

アルゴリズム分類

  • 数学
  • 四則演算

解説

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 {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            StringTokenizer input = new StringTokenizer(br.readLine(), " ");
            final long a = Long.parseLong(input.nextToken());
            final long b = Long.parseLong(input.nextToken());
            final long c = Long.parseLong(input.nextToken());

            if (b >= c) {
                System.out.println(-1);
                return;
            }

            int n = 0;
            long cost = 0;
            long income = 0;

            do {
                n++;

                cost = a + (b * n);
                income = c * n;

                //System.out.println("n=" + n + ", cost=" + cost + ", income=" + income);
            } while (cost >= income);

            System.out.println(n);
        }
    }
}
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 {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            StringTokenizer input = new StringTokenizer(br.readLine(), " ");
            final long a = Long.parseLong(input.nextToken());
            final long b = Long.parseLong(input.nextToken());
            final long c = Long.parseLong(input.nextToken());

            int count = -1;
            if (b < c) {
                count = (int) ((a / (c - b)) + 1);
            }

            System.out.println(count);
        }
    }
}