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

Posted at

AtCoder ABC 116 A&B&C

AtCoder - 116

A問題

  • $底辺 \times 高さ \div 2$
  • CAは使わない
	private static void solveA() {
		try (Scanner scanner = new Scanner(System.in)) {
			int lineAB = 0;
			int lineBC = 0;
			int lineCA = 0;

			lineAB = scanner.nextInt();
			lineBC = scanner.nextInt();
			lineCA = scanner.nextInt();

			int result = lineAB * lineBC / 2;
			System.out.println(result);

		}
	}

B問題

  • $f(n) $

    • n が偶数なら $f(n)=n/2$
    • n が奇数なら $f(n)=3n+1$
  • $i=1 のとき a_i=s、i>1 のとき a_i=f(a_{i−1}) $

  • $a_1=8$

  • $a_2=f(a_{2-1})=f(a_1)=f(8)$

  • $a_3=f(a_{3-1})=f(a_2)=f(4)$

	private static void solveB() {

		try (Scanner scanner = new Scanner(System.in)) {

			int numS = 0;
			numS = scanner.nextInt();

			int wkResult = 0;

			List<Integer> wkList = new ArrayList<Integer>();
			int cnt = 0;
			while (true) {
				if (cnt != 0) {
					/*
					 * isResultNum()でf(n)を計算する
					 */
					wkResult = isResultNum(wkList.get(cnt - 1));
					/*
					 * am=an(m>n)
					 * 現時点のcntの値より前に同じwkResultが出てきているのかをチェック
					 * 既におなじwkResultが既出ならcnt++してbreak(indexが0スタートなので1スタートに合わせる)
					 */
					if (wkList.contains(wkResult)) {
						cnt++;
						break;
					}
					wkList.add(wkResult);
				} else {
					wkResult = numS;
					wkList.add(numS);
				}
				cnt++;
			}
			System.out.println(cnt);
		}
	}

	private static int isResultNum(int numS) {
		if (numS % 2 == 0) {
			return numS / 2;
		} else {
			return 3 * numS + 1;
		}
	}

C問題

方針

  • 「0からスタートして入力値に合わせる」

よりは

  • 「入力値を0にしていくためにどうすればよいのか?」の方が考えやすいので入力値を0にする操作回数を求める
	private static void solveC() {

		try (Scanner scanner = new Scanner(System.in)) {
			int numN = 0;
			numN = scanner.nextInt();
			int[] arrayH = new int[numN];
			for (int i = 0; i < arrayH.length; i++) {
				arrayH[i] = scanner.nextInt();
			}

			int cnt = 0;
			/*
			 * 入力値がすべて0になるまで繰り返す
			 */
			while (isNotExit(arrayH)) {
				cnt++;
				boolean notZeroCnt = false;
				/*
				 * 水やり作業
				 */
				for (int i = 0; i < arrayH.length; i++) {
					/*
					 * 0に達したものはそれ以上水をやれないので、
					 * 0を見つけたら水やりの切れ目
					 */
					if (arrayH[i] != 0) {
						arrayH[i]--;
						notZeroCnt = true;
					} else {
						/*
						 * 0の箇所があるので水やりの切れ目なのだが、
						 * まだ1度も水を挙げていない場合
						 * |0|0|1|0|
						 * ↑みたいな場合、1,2個目は無視しないといけない
						 * notZeroCntはそのために利用している。
						 * もっとうまいやり方。。。
						 */
						if (notZeroCnt) {
							break;
						}
					}

				}
			}
			System.out.println(cnt);
		}
	}

	/*
	 * 高さ0より大きい入力値が存在するのかを確認する
	 */
	private static boolean isNotExit(int[] arrayH) {
		for (int i = 0; i < arrayH.length; i++) {
			if (arrayH[i] != 0) {
				return true;
			}
		}
		return false;
	}
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?