Baekjoon Algorithm | Problem 1065: Hansu

Source

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

Problem

Solve Baekjoon Online Judge problem 1065, Hansu.

Input

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

Output

Print the answer required by the problem.

Sample Input 1

110

Sample Output 1

99

Sample Input 2

1

Sample Output 2

1

Sample Input 3

210

Sample Output 3

105

Sample Input 4

1000

Sample Output 4

144

Sample Input 5

500

Sample Output 5

199

Algorithm Classification

  • Brute Force

Solution

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

public class Main {

    public static void main(String[] args) throws IOException {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            int n = Integer.parseInt(br.readLine());

            int count = 0;
            if (n < 100) {
                count = n;
            } else {
                count = 99;

                int loop = n;
                if (n == 1000)
                    loop = 999;

                for (int i = 100; i <= loop; i++) {

                    int a = i % 10;
                    int b = (i / 10) % 10;
                    int c = (i / 100) % 10;

                    if (a - b == b - c) {
                        count++;
                        //System.out.println("i=" + i + ", count=" + count + ", a=" + a + ", b=" + b + ", c=" + c + ", result=" + (a - b == b - c));
                    }

                }
            }

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