Baekjoon Algorithm | Problem 1712: Break-even Point

Source

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

Problem

Solve Baekjoon Online Judge problem 1712, Break-even Point.

Input

Follow the input format and constraints given in the original problem statement.

Output

Print the answer required by the problem.

Sample Input 1

1000 70 170

Sample Output 1

11

Sample Input 2

3 2 1

Sample Output 2

-1

Sample Input 3

2100000000 9 10

Sample Output 3

2100000001

Algorithm Classification

  • Math
  • Arithmetic

Solution

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);
        }
    }
}