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

Posted at

AtCoder ABC 045 B&C

AtCoder - 045

A問題

	private void solveA() {
		int numA = nextInt();
		int numB = nextInt();
		int numH = nextInt();

		out.println((numA + numB) * numH / 2);
	}

B問題

  • ループの中で、A,B,Cそれぞれの枚数が今何枚かをカウントしておく
	private void solveB() {
		char[] a = next().toCharArray();
		char[] b = next().toCharArray();
		char[] c = next().toCharArray();

		int cntA = 0;
		int cntB = 0;
		int cntC = 0;
		char num = 'a';
		while (true) {

			switch (num) {
			case 'a':
				if (cntA < a.length) {
					num = a[cntA];
					cntA++;
				} else {
					out.println("A");
					return;
				}
				break;
			case 'b':
				if (cntB < b.length) {
					num = b[cntB];
					cntB++;
				} else {
					out.println("B");
					return;
				}
				break;
			case 'c':
				if (cntC < c.length) {
					num = c[cntC];
					cntC++;
				} else {
					out.println("C");
					return;
				}
				break;
			}
		}

	}

C問題

  • 以下のような感じで10個の数値の間に $+$ を入れていく
数列:
1234567891
-> 1 + 23456789
-> 1 + 2 + 3456789
-> 1 + 2 + 3 + 456789
-> 1 + 2 + 3 + 4 + 56789
  ・
  ・
-> 1234 + 56789
  ・
  ・
-> 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
  • $+$ が数式の間に挿入されるパターンは $2^n$ になる

    • $n$ は最小0:最大9
  • このパターンの作成は、マスクを作る時のやり方を使うと楽

    • 000000000 -> 000000001 -> 000000010 -> 000000011 -> ・・・ -> 111111111
    • 0は何もしない
    • 1は $+$ として扱う

マスクの作成部分だけ抜き出し

  • 0-maxまで、2進数に変換して配列に入れる
		for (int i = 0; i < max; i++) {
			int[] plusMinus = new int[maskMax - 1];
			int wkBit = i;
			for (int j = 0; j < maskMax - 1; j++) {
				plusMinus[j] = wkBit % 2;
				wkBit /= 2;
			}
		}

C問題:解答本体

	private long chkC(String[] number) {
		long res = 0;
		/*
		 * +-は最大9個
		 * 000000000から、111111111までの配列を作成。2の剰余を入れていけば2進数に変換できる。
		 * 最大は 2^9
		 * 最小は 2^0
		 *
		 */
		int maskMax = number.length - 1;

		/*
		 * マスクの最大値を設定
		 * ex:1024 -> 1111111111
		 */
		int max = (int) Math.pow(2, maskMax);
		for (int i = 0; i < max; i++) {
			/*
			 * bitを生成
			 * 1は+/0は何もしない
			 * 例:数値が123の場合
			 * 00
			 * 01
			 * 10
			 * 11
			 * の4パターン生成される
			 * 00の場合は数値をそのまま利用
			 */
			int[] plusMinus = new int[maskMax];
			int wkBit = i;
			for (int j = 0; j < maskMax; j++) {
				plusMinus[j] = wkBit % 2;
				wkBit /= 2;
			}
			StringBuilder builder = new StringBuilder();
			/*
			 * 数値を文字列に入れた後、bitが1なら+(扱いの@)を足す
			 * +を足さないのはあとの処理で正規表現扱いになるのを避けるため
			 */
			for (int j = 0; j < number.length; j++) {
				builder.append(number[j]);
				if (j < plusMinus.length && plusMinus[j] == 1) {
					builder.append("@");
				}
			}
			/*
			 * @で区切って数値に変換
			 */
			String[] numS = builder.toString().split("@");
			//足す
			for (String long1 : numS) {
				res += Long.parseLong(long1);
			}
		}

		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?