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

Posted at

AtCoder ABC 070 A&B&C

AtCoder - 070

A問題

  • 数値の比較ではなく文字列で比較。StringBuilder#reverseで逆順に変換
    • 便利なんだけど速度は遅い
	private void solveA() {
		String numN = next();

		out.println(numN.equals(new StringBuilder(numN).reverse().toString()) ? "Yes" : "No");
	}

B問題

  • A,B,C,Dの組み合わせは以下になる
    • (A↔C)(B↔D)の位置は変換可
    • ● の位置が重なる秒数(と思って)
--- --- --- --- ---
A B
C D
--- --- --- --- ---
A B
C D
--- --- --- --- ---
A B
C D
--- --- --- --- ---
A B
C D
	private void solveB() {
		int numA = nextInt();
		int numB = nextInt();
		int numC = nextInt();
		int numD = nextInt();

		int wkRes = Math.min(numB, numD) - Math.max(numA, numC);
		//wkReが0より小さい場合、二人がスイッチを押していた時間の被りはない
		out.println(wkRes >= 0 ? wkRes : 0);
	}

C問題

  • この時計達の最小公倍数を求めれば、もう一度すべての針が上を向く
    • 最小公倍数=$(val1 * val2)/(val1とval2の最大公約数)$
	private void solveC() {
		int numN = nextInt();

		BigDecimal[] times = new BigDecimal[numN];
		for (int i = 0; i < numN; i++) {
			times[i] = new BigDecimal(next());
		}


		/*
		 * 1つしか要素がない場合は要素の秒数がかかる秒数
		 */
		if (times.length == 1) {
			out.println(times[0]);
			return;
		}

		/*
		 * 最小公倍数を求める
		 */
		BigDecimal res = BigDecimal.ZERO;
		for (int i = 1; i < times.length; i++) {
			if (i == 1) {
				res = getMinKoubaisu(times[i - 1], times[i]);
			} else {
				res = getMinKoubaisu(res, times[i]);
			}
		}

		out.println(res.longValue());
	}

	private BigDecimal getMinKoubaisu(BigDecimal val1, BigDecimal val2) {
		BigDecimal wk = getMaxKouyaku(val1, val2);
		return (val1.multiply(val2).divide(wk));
	}

	private BigDecimal getMaxKouyaku(BigDecimal val1, BigDecimal val2) {

		BigDecimal wkVal1 = val1.compareTo(val2) <= 0 ? val1 : val2;
		BigDecimal wkVal2 = val1.compareTo(val2) > 0 ? val1 : val2;
		BigDecimal amari = wkVal2.remainder(wkVal1);
		while (!amari.equals(BigDecimal.ZERO)) {
			wkVal2 = wkVal1;
			wkVal1 = amari;
			amari = wkVal2.remainder(wkVal1);
		}

		return wkVal1;
	}
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?