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

Last updated at Posted at 2019-05-16

AtCoder ABC 019 A&B&C

AtCoder - 019

今更ながら問題名がないと自分が探しづらいことに気付いたのでこれから問題名を埋めていこう。

2019/05/17:AとBのコードを修正

A - 高橋くんと年齢

  • sortはstreamで共に処理可能とコメントでご指摘いただいたためコード修正

    • sortも同時にできるのか。。。やれること多すぎて理解追っつかないわ。
  • 中央値なのでソートして真ん中

	private void solveA() {
		int[] wk = IntStream.range(0, 3).map(i -> nextInt()).sorted().toArray();

		out.println(wk[1]);
	}

old

private void solveA() {

	int[] wk = IntStream.range(0, 3).map(i -> nextInt()).toArray();

	Arrays.sort(wk);

	out.println(wk[1]);
}

B - 高橋くんと文字列圧縮

  • 2019/05/19追記:コメント欄に@swordone様のコードあり。そちらの方がシンプル。

  • 同じ文字列がいくつ連続しているのを数えてappend

    • ループ抜けた後cntが0になっている=最後の1文字は最後-1文字とは違う文字なのでカウント
    • ループ抜けた後cntが0ではない=最後は同じ文字なので++
	private void solveB() {
		char[] wk = next().toCharArray();
		StringBuilder builder = new StringBuilder();
		int cnt = 0;
		for (int i = 0; i < wk.length - 1; i++) {
			if (cnt == 0) {
				builder.append(wk[i]);
				cnt++;
			}
			if (wk[i] != wk[i + 1]) {
				builder.append(cnt);
				cnt = 0;
			} else {
				cnt++;
			}
		}
		if (cnt != 0) {
			builder.append(cnt);
		} else {
			builder.append(wk[wk.length - 1]);
			builder.append(1);
		}

		out.println(builder.toString());

	}

C - 高橋くんと魔法の箱:setとlist利用

  • $x入れても2x入れても同じ整数$
    • $x,2x,4x,8x・・・$となる値は1としてカウント
  • setにもlistにも同じ値を入れて、listをベースにループさせてsetから値を取り除いていけば。
	private void solveC() {
		Set<Long> wk = new HashSet<Long>();
		List<Long> base = new ArrayList<Long>();
		int n = nextInt();
		for (int i = 0; i < n; i++) {
			long val = nextLong();
			wk.add(val);
			base.add(val);
		}

		Collections.sort(base);
		long max = base.get(base.size() - 1);
		long res = 0;
		for (Long longVal : base) {
			if (wk.contains(longVal)) {
				wk.remove(longVal);
				res += recursiveC(wk, max, longVal);
			}
			if (wk.size() == 0) {
				break;
			}
		}

		out.println(res);
	}

	private int recursiveC(Set<Long> wk, long max, long current) {
		if (current > max || wk.size() == 0) {
			return 1;
		}

		current <<= 1;
		wk.remove(current);
		return recursiveC(wk, max, current);
	}

C - 高橋くんと魔法の箱:setのみ利用

  • ループでACしたあと、「これ、結局は奇数の個数をかぞえているだけでは?」となったので試してみたらAC
    • $1を2倍している数は2で割っていったら最後は1になる$
    • $奇数を2倍したら必ず偶数$
	private void solveC2() {
		Set<Integer> wk = new HashSet<Integer>();
		int n = nextInt();
		for (int i = 0; i < n; i++) {
			int val = nextInt();
			while ((val & 1) != 1) {
				val /= 2;
			}
			wk.add(val);
		}

		out.println(wk.size());
	}
0
0
2

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?