3
0

問題文

解答例

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int b = scanner.nextInt();
			int n = scanner.nextInt();
			for (int i = 0; i < n; i++) {
				int a = scanner.nextInt();
				if (a == b) {
					System.out.println("first");
				} else if ((a + 1) == b || (a - 1) == b) {
					System.out.println("adjacent");
				} else if (a % 10000 == b % 10000) {
					System.out.println("second");
				} else if (a % 1000 == b % 1000) {
					System.out.println("third");
				} else {
					System.out.println("blank");
				}
			}
		}
	}
}

補足

下4桁が一致するかどうかは10000で割った余りを確認すればよい(下3桁のチェックも同様)。
当たり前だが1等は2等と3等の条件も満たし、2等は3等の条件も満たす。そのため条件の厳しいものから先にチェックしないと正しい解答を得られないため注意する必要がある。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0