0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ABC - 097- A&B&C

Last updated at Posted at 2019-04-08

AtCoder ABC 097 A&B&C

AtCoder - 097

A問題

  • A,B,C間の距離がD以下であれば通話可能
	private void solveA() {
		Scanner scanner = null;
		int numA = 0;
		int numB = 0;
		int numC = 0;
		int numD = 0;

		try {
			scanner = new Scanner(System.in);
			numA = scanner.nextInt();
			numB = scanner.nextInt();
			numC = scanner.nextInt();
			numD = scanner.nextInt();

			boolean canTalk = false;

			canTalk = ((Math.abs(numA - numB) <= numD && Math.abs(numB - numC) <= numD)
					|| Math.abs(numA - numC) <= numD);

			System.out.println(canTalk ? "Yes" : "No");

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

B問題

  • X以下の最大のべき乗数を求める
  • 簡略化しようとすると変な回答をしてしまうので、おとなしく全探索をする
  • $i^j \leqq X$をループで回し、X以下の最大を求める
    • $i \leqq X$ となる。 $X$ 超えたら終了という条件
	private void solveB() {
		Scanner scanner = null;
		int numX = 0;

		try {
			scanner = new Scanner(System.in);
			numX = scanner.nextInt();

			int max = 0;
			for (int i = 1; i <= numX; i++) {
				for (int j = 2; j < 10; j++) {
					if (Math.pow(i, j) <= numX) {
						max = Math.max(max, (int) Math.pow(i, j));
					} else {
						break;
					}
				}
			}

			System.out.println(max);
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

C問題

文字列を切り出す作業を実際にやってみると、文字列長はKまででいいことがわかる。
文字列長がKより長い文字列はK番目以内に来ることができない

  • 文字列は連続した値なので以下の操作でよい
    • 文字列のindex=0から1-K文字切り出して
    • index=1から1-K文字切り出して
    • index=2から1-K文字切り出して
文字列:abcoder
長さK:6

a
ab
abc
abco
abcod
--abcode
--abcoder
b
bc
bco
bcod
bcode
--bcoder
co
cod
code
coder
	/**
	 * 文字列を切り出す作業を実際にやってみると、文字列長はnumKまででいいことがわかる。
	 *abcoder
	*
	*a
	*ab
	*abc
	*abco
	*abcod
	*--abcode
	*--abcoder
	*b
	*bc
	*bco
	*bcod
	*bcode
	*--bcoder
	*co
	*cod
	*code
	*coder

	*/

	private void solveC() {
		Scanner scanner = null;
		String s = "";
		int numK = 0;

		try {
			scanner = new Scanner(System.in);
			s = scanner.next();
			numK = scanner.nextInt();
			/*
			 * 切り出し時の重複除外のためsetを利用
			 */
			Set<String> wk = new HashSet<String>();

			String wkS = "";
			for (int i = 0; i < s.length(); i++) {
				int max = i + numK < s.length() ? i + numK : s.length();
				for (int j = i; j <= max; j++) {
					wkS = s.substring(i, j);
					if (wkS != null && !wkS.equals("")) {
						//重複の除外
						wk.add(wkS);
					}
				}
			}

			/*
			 * 入れなおし
			 */
			List<String> wkList = new ArrayList<String>();
			for (String string : wk) {
				wkList.add(string);
			}

			/*
			 * 文字順にソート
			 */
			Collections.sort(wkList);

			System.out.println(wkList.get(numK - 1));

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}


0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?