過去問精選 10 問やってみる 5
前回の続きです
ABC 080 B - Harshad Number (同じく整数の 10 進法表記に関する問題です)
https://atcoder.jp/contests/abc080/tasks/abc080_b
与えられた数値を文字列として1桁ずつ処理して、判定する
N = input() sum = 0 for i in N: sum += int(i) if int(N) % sum == 0: print('Yes') else: print('No')
ABC 090 B - Palindromic Numbers (同じくです)
https://atcoder.jp/contests/abc090/tasks/abc090_b
これも数値を文字列として受けて、5桁縛りなので桁固定で一致を確認する a, b = map(int, input().split()) count = 0 for i in range(a, b+1): print(i) A = str(i) if A[0] == A[-1] and A[1] == A[-2]: count += 1 print(count)
AGC 025 A - Digits Sum (同じくです)
https://atcoder.jp/contests/agc025/tasks/agc025_a
最初、時短のためにforのレンジを半分にしていたのですが、WAとなってしまいました
どんなテストケースで失敗したんだろう・・・
N = int(input()) min = 100000000000000 for i in range(1, N): A = str(i) B = str(N - i) ref = 0 for a in A: ref += int(a) for b in B: ref += int(b) if ref < min: min = ref print(min)
**************************************************************************
ABC 067 B - Snake Toy (ソートです)
https://atcoder.jp/contests/abc067/tasks/abc067_b
並び変えてK番目までを足すだけ
N, K = map(int, input().split()) a = list(map(int, input().split())) max = 0 a.sort(reverse=True) for i in a[0:K]: max += i print(max)
ABC 042 B - Iroha Loves Strings (ソートする対象が文字列になります)
https://atcoder.jp/contests/abc042/tasks/abc042_b
文字列をソートして一つの文字列に合体させたものを出力する
N, L = map(int, input().split()) S = [input() for _ in range(N)] S.sort() print("".join(S))
AGC 027 A - Candy Distribution Again (ソートして小さい順に配って行きます)
https://atcoder.jp/contests/agc027/tasks/agc027_a
配った後に、いくつかの条件をつけてカウントを減らす処理を入れた
N, x = map(int, input().split()) a = list(map(int, input().split())) p = x a.sort() count = 0 for i in a: if x - i >= 0: x -= i count += 1 if x > 0 and count != 0 and p > sum(a): count -= 1 print(count)
AGC 012 A - AtCoder Group Contest (少し難しめのソート問題です、是非挑戦してみましょう)
https://atcoder.jp/contests/agc012/tasks/agc012_a
Nがどのような値でも、降順に並び替えて奇数偶数の順で固まりを作ると
偶数側*N個の合計値が求める値となる
N = int(input()) a = list(map(int, input().split())) a.sort(reverse=True) max = 0 for i in range(N*2): if (i+1) % 2 == 0: max += a[i] print(max)
以上