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 - 063- A&B&C

Last updated at Posted at 2019-03-28

AtCoder ABC 063 A&B&C

AtCoder - 063

A問題

  • $A + B \geqq 10$ を判定
	private void solveA() {
		int numA = nextInt();
		int numB = nextInt();

		if (numA + numB >= 10) {
			out.println("error");
		} else {
			out.println(numA + numB);
		}

	}

B問題

  • 文字列を全て分解して出現回数を調べる
    • Mapを使っても良かったのだけど配列で実装してみたかった
    • $(cahr)'XXX' - (char)'a'$ でint[26]の場所を決めるというのをやってみたかったので
	private void solveB() {
		char[] wkC = next().toCharArray();
		int[] wk = new int[26];

		for (int i = 0; i < wkC.length; i++) {
			int index = wkC[i] - (char) 'a';
			wk[index] += 1;
		}
		Arrays.sort(wk);
		out.println(wk[25] > 1 ? "no" : "yes");
	}

C問題

  • 先ず全部足してしまう

  • 足した結果が10の倍数でない場合**(sum % 10 != 0)**

    • それが回答
  • 足した結果が10の倍数の場合**(sum % 10 == 0)**

    • (合計-テストの点数) % 10 != 0 になる最小値を探す
    • テストの配点配列を元に、一番点数が低い配点から順に探す
private void solveC() {
		int numN = nextInt();
		int[] wkC = IntStream.range(0, numN).map(i -> nextInt()).toArray();

		int cnt = Arrays.stream(wkC).sum();

		/*
		 * 配点の合計点数が10の倍数でない場合はそれが答え
		 */
		if (cnt % 10 != 0) {
			out.println(cnt);
			return;
		}

		/*
		 * 配点配列を配点が少ない順にソート
		 */
		Arrays.sort(wkC);

		/*
		 * wkC[i]%10 != 0で判定をするとNG
		 *  13の時、-3したら10になってしまう。
		 *  (cnt-wkC[i]) %10 != 0で判定をする
		 *   13-3=10を除外するため
		 *  何故か気づかずはまった orz
		 *
		 */
		for (int i = 0; i < wkC.length; i++) {
			int wkCnt = cnt - wkC[i];
			if (wkCnt % 10 != 0) {
				out.println(wkCnt);
				return;
			}
		}
		out.println(0);

		//		out.println(chkC(wkC, 0, 0, numN));
	}

C問題のTLEになるような実装

  • 再帰使って全部走査することをやってみたら、、、TLEになった。
  • 時間気にしない場合これでも動作はする。
	private long chkC(int[] wkC, int currentI, long total, int max) {

		if (currentI >= max) {
			if (total % 10 == 0) {
				return 0;
			} else {
				return total;
			}
		}
		long res = 0;

		//自分を足して次に進む場合
		long val1 = chkC(wkC, currentI + 1, total + wkC[currentI], max);
		//自分を足さないで次に進む場合
		long val2 = chkC(wkC, currentI + 1, total, max);

		res = Math.max(val1, val2);
		//		out.println(currentI + " : " + total);
		return res;
	}
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?