LoginSignup
0
0

More than 3 years have passed since last update.

Python B問題対策(Atcorder)

Last updated at Posted at 2019-12-17

for文問題

N=input()

cnt=0
for i in range(len(N)):#書き方忘れてた...
    cnt+=int(N[i]) #int型にしてなくて、躓いてた(笑)

if int(N)%cnt==0:
    print("Yes")
else:
    print("No")

N=int(input())
A=list(map(int,input().split()))#一旦、入力を全て終了させる!
ans=0

print(A)

for a in A:
    cnt=0
    while(a%2==0):
        a/=2
        cnt+=1
    ans+=cnt
print(ans)

#入力 
10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

#出力
[2184, 2126, 1721, 1800, 1024, 2528, 3360, 1945, 1280, 1776]
39

以下のfor文を使えるようにする!
image.png
image.png

image.png

配列問題

N=int(input())
L=[2,1]#配列初期化、宣言同時に!

for i in range(2,N+1):
    L.append(L[i-1]+L[i-2])#末尾に追加!
print(L[N])

辞書問題

N=int(input())

dic={}
for i in range(N):
    tmp="".join(sorted(input()))#リストを文字列に変換する
    if tmp in dic.keys():
        dic[tmp]+=1
    else:
        dic[tmp]=1#key値,value値を同時に辞書の中に入れてる!
print(dic)

ans=0
for i in dic.values():
    ans+=i*(i-1)//2
print(ans)

#入力
5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

#出力
{'aaaaaaaaab': 3, 'elnoopstuw': 2}
4
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