ABC 208 B – Factorial Yen Coin 解説
def calc(i):
cnt = 0
total = 1
while i>0:
total *= i
i -= 1
cnt += 1
return total
p = int(input())
count = 0
for i in range(10,0,-1):
while calc(i) <= p:
count += 1
p -= calc(i)
print(count)
ABC 169 A – Multiplication 1
a,b = map(int, input().split()) print(a*b)
ABC 165 A – We Love Golf breakで繰り返し処理を終わらせる
k = int(input())
a,b = map(int, input().split())
ans = ""
for i in range(a,b+1):
if i%k==0:
ans = "OK"
break
else:
ans = "NG"
print(ans)
ABC 158 C – Tax Increase 1<=A<=B<=100 なので、税抜き価格は最大でも1,000
a,b = map(int, input().split())
result = -1
for i in range(1,1001):
tax8in = int(i*0.08)
tax10in = int(i*0.1)
if tax8in==a and tax10in==b:
result = i
break
print(result)
ABC 153 A – Serval vs Monster while文の基本
h,a = map(int, input().split()) cnt = 0 while h > 0: h -= a cnt += 1 print(cnt)
ABC 104 B – AcCepted 文字列スライス 文字列検索 文字数カウントs.count() 小文字カウント sum(map(str.islower, s))
s = input()
a = "A" in s[0]
c = "C" in s[2:-1]
d = False
if s.count("C")==1:
d = True
cnt = False
if len(s) == sum(map(str.islower, s))+2:
cnt = True
lst=[a,c,d,cnt]
if all(lst)==True:
print("AC")
else:
print("WA")
ABC 093 B – Small and Large Integers 計算量の重要性 forループ総当たりはムダ
a,b,k = map(int, input().split()) c = (b-a)/2 # a-b範囲内の個数 if c<k: # 個数がkより小さければ、全部出力 lst = [i for i in range(a,b+1)] else: lst = [i for i in range(a,a+k)] + [i for i in range(b-k+1, b+1)] for i in lst: print(i)
ABC 088 B – Card Game for Two sorted()の基本
n = int(input()) a = list(map(int,input().split())) alice = 0 bob = 0 lst = sorted(a, reverse=True) for i in range(0,len(lst),2): alice += lst[i] for i in range(1,len(lst),2): bob += lst[i] print(alice-bob)
ABC 087 B – Coins 組合せ
a = int(input())
b = int(input())
c = int(input())
x = int(input())
cnt = 0
for i in range(a+1):
for j in range(b+1):
for k in range(c+1):
if i*500 + j*100 + k*50 == x:
cnt += 1
print(cnt)
ABC 086 A – Product if文の基本
a,b = map(int, input().split())
if a*b%2 != 0:
print("Odd")
else:
print("Even")
ABC 083 B – Some Sums
# 各桁の総和を求める関数
def sum_digit(x):
digit=0
while x>0:
digit += x%10
x //= 10
return digit
n,a,b = map(int, input().split())
total = 0
for i in range(1,n+1):
if a<= sum_digit(i) <= b:
total += i
print(total)
ABC 081 B – Shift only while all() for文の内包表記でリストに改変
n = int(input()) a = list(map(int,input().split())) cnt = 0 while all(i%2==0 for i in a): a = [i//2 for i in a] cnt += 1 print(cnt)
ABC 075 B – Minesweeper 複数行入力をリスト化 ””.join(lst) 指定した文字数で分割して2次元リスト化
h,w = map(int, input().split())
s = [list(input()) for _ in range(h)] # 複数行入力を2次元リスト化
lst = []
for i in range(h):
for j in range(w):
cnt = 0
if s[i][j]=="#":
cnt = "#"
lst.append(cnt)
else:
if i!=0 and j!=0 and s[i-1][j-1]=="#": # 左上
cnt += 1
if i!=0 and s[i-1][j]=="#": # 真中上
cnt += 1
if i!=0 and j<w-1 and s[i-1][j+1]=="#": # 右上
cnt += 1
if 0<j and s[i][j-1]=="#": # 左
cnt += 1
if j<w-1 and s[i][j+1]=="#": # 右
cnt += 1
if i<h-1 and j!=0 and s[i+1][j-1]=="#": # 左下
cnt += 1
if i<h-1 and s[i+1][j]=="#": # 真中下
cnt += 1
if i<h-1 and j<w-1 and s[i+1][j+1]=="#": # 右下
cnt += 1
lst.append(str(cnt))
lst = "".join(lst)
lst = [lst[i:i+w] for i in range(0,len(lst),w)] # 指定した文字数で分割して2次元リスト化
for i in range(len(lst)):
print(lst[i])
# 1行ずつ1次元リスト化 ※行数が判らない場合
import sys
s = []
for e in sys.stdin:
s.append(e.rstrip("\n")
>['…..', '.#.#.', '…..']
# 1文字ずつ分けて2次元リスト化(1) ※行数が判っている場合
s = [list(input()) for _ in range(h)]
>[['.', '.', '.', '.', '.'], ['.', '#', '.', '#', '.'], ['.', '.', '.', '.', '.']]
# 1文字ずつ分けて2次元リスト化(2) ※行数が判っている場合
s = [[e for e in input()] for _ in range(h)]
>[['.', '.', '.', '.', '.'], ['.', '#', '.', '#', '.'], ['.', '.', '.', '.', '.']]
## Welcome to AtCoder
### a+b+c の計算結果と、文字列s を並べて表示
a = int(input())
b,c = map(int, input().split()) ★標準入力を分割して変数に入れる★超よく使う
s = input()
print("{} {}".format(a+b+c, s)) ★formatの各引数(計算結果)を順に{}に入れる★超よく使う
## Product
### a と b の積が偶数か奇数か判定
a,b = map(int, input().split())
if a*b % 2 == 0:
print("Even")
else:
print("Odd")
## Placing Marbles
### ビー玉が置かれるマスがいくつあるか求める
s1,s2,s3 = list(map(int, input()))
print(s1+s2+s3)
## Shift only
### 整数がすべて偶数であるとき、整数すべてを2で割ったものに置き換えることができる。ビー玉が置かれるマスがいくつあるか
n = int(input())
a = list(map(int, input().split()))
cnt = 0
while all(i%2==0 for i in a): ★全ての要素が偶数の場合 all()関数の内包表記
a = [j/2 for j in a] ★全ての要素を2で割る 内包表記
cnt+=1
print(cnt)
## Coins
### 500円玉をA枚、100円玉をB枚、50円玉をC枚持っています。何枚かを選び、合計金額をちょうどX円にする方法は何通りあるか。
a = int(input())
b = int(input())
c = int(input())
x = int(input())
count = 0
for i in range(a+1):
for j in range(b+1):
for k in range(c+1):
if i*500 + j*100 + k*50 == x:
count += 1
print(count)
## Some Sums
### 1以上N以下の整数のうち、10進法での各桁の和がA以上B以下であるものの総和を求めよ
~~~~~~~~~
def calc(n):
sum_digit=0
while n>0:
sum_digit += n%10
n //= 10
return sum_digit
n,a,b = map(int, input().split())
total = 0
for i in range(1,n+1):
if a<=calc(i)<=b:
total += i
print(total)
~~~~~~~~
n,a,b = map(int, input().split()) ★標準入力を分割して変数に入れる★超よく使う
total = 0
for i in range(1,n+1):
s = sum(map(int, str(i))) ★各桁の数字を足す
if a <= s <= b:
total += i
print(total)
~~~~~~~~~
x = list("111")
> ['1', '1', '1']
> str
x = list(map(int,"111"))
> [1, 1, 1]
> int
~~~~~~~~~
ナベアツ
for i in range(1,41):
if i%3 == 0 or 3 in list(map(int,str(i))):
print(i,"(アホ)")
else:
print(i)
[8]
[9]
[1, 0]
[1, 1]
~~~~~~~~~
## Card Game for Two
### N枚のカードから、AliceとBobが交互に1枚ずつカードを取っていき、合計がその人の得点になる。
### 2人とも自分の得点を最大化するように最適な戦略を取った時、AliceはBobより何点多く取るか。
n = int(input())
a = sorted(list(map(int, input().split())),reverse=True)
alice = a[0:n:2]
bob = a[1:n:2]
print(sum(alice)-sum(bob))
## Kagami Mochi
### N枚の円形の餅があり、そのうちi枚目の餅の直径はd1,d2...センチメートル。
### これらの餅のうち、一部または全部を使って鏡餅を作るとき、最大で何段重ねの鏡餅を作ることができるでしょうか
import sys
n = int(input()) ----------------
d = [] 縦に標準入力された値を
for i in sys.stdin: 1つのリストに収める方法
d.append(int(i)) --------------
print(len(list(set(d)))) #重複なしリストにして、要素の数をカウント
## Otoshidama
### お年玉袋にはお札がN枚入っていて、合計でY円だったそうですが、嘘かもしれません。
### このような状況がありうるか判定し、ありうる場合はお年玉袋の中身の候補を一つ見つけてください。
### N 枚のお札の合計金額がY円となることがありえない場合は、-1 -1 -1 と出力せよ。
### ありうる場合は、そのようなN枚のお札の組合せの一例を「10000円札x枚、5000円札y枚、1000円札z枚」として
### x、y、zを空白で区切って出力せよ。複数の可能性が考えられるときは、そのうちどれを出力してもよい。
n,y = map(int, input().split())
for i in range(n+1): #10000円札
for j in range(n+1-i): #5000円札
k = n-i-j #1000円札
if i*10000 + j*5000 + k*1000 == y:
print("{} {} {}".format(i,j,k))
exit()
print("-1 -1 -1")
## 白昼夢
## 英小文字からなる文字列 S が与えられます。
## Tが空文字列である状態から始め、以下の操作を好きな回数繰り返すことで S=T とすることができるか判定してください。
## T の末尾に dream dreamer erase eraser のいずれかを追加する。
s = input()
s = s.replace("eraser","")
s = s.replace("erase","")
s = s.replace("dreamer","")
s = s.replace("dream","")
if s=="":
print("YES")
else:
print("NO")
発想の転換(文字の重なりを考慮して順番に消していって空白になれば判定Trueということ)
※itertools.permutations()は、結果がタプル型で返ってきてしまうので''.join()が使えないし、
itertools.chainl.from_iterable()にも勧めない)
## Traveling
## シカのAtCoDeerくんは二次元平面上で旅行をしようとしています。 AtCoDeerくんの旅行プランでは、
## 時刻0 に 点(0,0) を出発し、1以上 N以下 の各i に対し、時刻ti に 点(xi ,yi) を訪れる予定です。
## AtCoDeerくんが時刻 t に 点 (x,y) にいる時、時刻 t+1 には 点 (x+1,y), (x−1,y), (x,y+1), (x,y−1) のうちいずれかに存在することができます。
## その場にとどまることは出来ないことに注意してください。 AtCoDeerくんの旅行プランが実行可能かどうか判定してください。