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

Posted at

AtCoder ABC 079 A&B&C

AtCoder - 079

A問題

  • 同じ数値が連続3回以上続くかを判定
    • Aにしてはコードが長い・・・
  • 多分、char[]にしてMapにputしてcountするでもよかったか
private void solveA() {
		int numN = nextInt();

		out.println(chkA(numN) ? "Yes" : "No");
	}

	private boolean chkA(int num) {

		boolean isGoodnum = false;
		int cnt = 0;
		int previous = 0;
		for (int i = 0; num != 0; i++) {
			if (i == 0) {
				//初期化
				previous = num % 10;
				num /= 10;
				cnt = 1;
			} else {
				int current = num % 10;
				if (previous == current) {
					cnt++;
				} else {
					//countやり直し
					cnt = 1;
				}
				previous = current;
				num /= 10;
			}
			if (cnt >= 3) {
				isGoodnum = true;
			}
		}
		return isGoodnum;
	}

B問題

  • {$L_0=2,L_1=1$}だけ先に定義
  • 後は問題どおり
  • ただし、{$N=1$}の時だけ{$1$}を出力するよう分岐
	private void solveB() {
		int numN = nextInt();

		if (numN == 1) {
			out.println(1);
			return;
		}

		long val1 = 2;
		long val2 = 1;
		long res = 0;
		for (int i = 0; i < numN - 1; i++) {
			res = val1 + val2;
			long temp = val1;
			val1 = val2;
			val2 = val2 + temp;
		}

		out.println(res);
	}

C問題

  • operandを$+/-$を全パターン試して合計が7になるのか調べる
private void solveC() {

		int num = nextInt();

		int[] args = convC(num);

		int[] wk = new int[4];

		int res = chkC(args, 0, wk, 0, 0);

		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < wk.length; i++) {
			if (i == 0) {
				builder.append(args[i]);
			} else {
				switch (wk[i]) {
				case 1:
					builder.append("+");
					break;
				case 2:
					builder.append("-");
					break;
				}
				builder.append(args[i]);
			}
		}

		builder.append("=7");
		out.println(builder.toString());
	}

	/**
	 * 数値を配列に変換
	 * @param num
	 * @return
	 */
	private int[] convC(int num) {
		int[] wk = new int[4];
		for (int i = wk.length - 1; i >= 0; i--) {
			wk[i] = num % 10;
			num /= 10;
		}
		return wk;
	}

	/**
	 * 再帰で加減算を行う
	 * マジックナンバーだが、+ => 1/- => 2
	 * total=7になったら終了
	 * @param args
	 * @param total
	 * @param wk
	 * @param currentI
	 * @param operand
	 * @return
	 */
	private int chkC(int[] args, int total, int[] wk, int currentI, int operand) {

		if (currentI >= 4) {
			return total;
		}
		if (currentI == 0) {
			total += args[0];
		} else {
			switch (operand) {
			case 1:
				total += args[currentI];
				wk[currentI] = operand;
				break;
			case 2:
				total -= args[currentI];
				wk[currentI] = operand;
				break;
			}
		}

		int val1 = chkC(args, total, wk, currentI + 1, 1);
		if (val1 == 7) {
			return 7;
		}
		int val2 = chkC(args, total, wk, currentI + 1, 2);
		if (val2 == 7) {
			return 7;
		}

		return -1;

	}
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?