AOJ ITP1 in Python

AOJ-ITP1を Python で解く.

# ITP1_1_A:

答え
1
print("Hello World")

# ITP1_1_B:

答え
1
2
x = int(input())
print(x ** 3)

# ITP1_1_C:

答え
1
2
h, w = map(int, input().split())
print(h * w, 2 * (h + w))

# ITP1_1_D:

答え
1
2
S = int(input())
print("{}:{}:{}".format(S // 3600, (S % 3600) // 60, S % 60))

# ITP1_2_A:

答え
1
2
3
4
5
6
7
a, b = map(int, input().split())
if a < b:
    print("a < b")
elif a == b:
    print("a == b")
else:
    print("a > b")

# ITP1_2_B:

答え
1
2
3
4
5
a, b, c = map(int, input().split())
if a < b < c:
    print("Yes")
else:
    print("No")

# ITP1_2_C:

答え
1
2
3
lst = list(map(int, input().split()))
lst.sort()
print(*lst)

# ITP1_2_D:

答え
1
2
3
4
5
W, H, x, y, r = map(int, input().split())
if r <= x <= W - r and r <= y <= H - r:
    print("Yes")
else:
    print("No")

# ITP1_3_A:

答え
1
2
for _ in range(1000):
    print("Hello World")

# ITP1_3_B:

答え
1
2
3
4
5
6
7
i = 1
while True:
    x = int(input())
    if x == 0:
        break
    print("Case {}: {}".format(i, x))
    i += 1

# ITP1_3_C:

答え
1
2
3
4
5
6
7
while True:
    x, y = map(int, input().split())
    if x == 0 and y == 0:
        break
    if y < x:
        x, y = y, x
    print(x, y)

# ITP1_3_D:

答え
1
2
3
4
5
6
a, b, c = map(int, input().split())
cnt = 0
for i in range(a, b + 1):
    if c % i == 0:
        cnt += 1
print(cnt)

# ITP1_4_A:

答え
1
2
a, b = map(int, input().split())
print(a // b, a % b, "{:.6f}".format(a / b))

# ITP1_4_B:

答え
1
2
3
from math import pi
r = int(input())
print("{:.6f} {:.6f}".format(2 * pi * r, pi * r ** 2))

# ITP1_4_C:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
while True:
    s = input().split()
    lhs = int(s[0])
    op = s[1]
    rhs = int(s[2])
    if op == "?":
        break
    if op == "+":
        print(lhs + rhs)
    elif op == "-":
        print(lhs - rhs)
    elif op == "*":
        print(lhs * rhs)
    elif op == "/":
        print(lhs // rhs)

# ITP1_4_D:

答え
1
2
3
_ = int(input())
lst = list(map(int, input().split()))
print(min(lst), max(lst), sum(lst))

# ITP1_5_A:

答え
1
2
3
4
5
6
7
while True:
    h, w = map(int, input().split())
    if h == 0 and w == 0:
        break
    for _ in range(h):
        print("#" * w)
    print()

# ITP1_5_B:

答え
1
2
3
4
5
6
7
8
9
while True:
    h, w = map(int, input().split())
    if h == 0 and w == 0:
        break
    print("#" * w)
    for _ in range(h - 2):
        print("#" + "." * (w - 2) + "#")
    print("#" * w)
    print()

# ITP1_5_C:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
while True:
    H, W = map(int, input().split())
    if H == 0 and W == 0:
        break
    for h in range(H):
        for w in range(W):
            if (h + w) % 2 == 0:
                print("#", end="")
            else:
                print(".", end="")
        print()
    print()

# ITP1_5_D:

答え
1
2
3
4
5
N = int(input())
for i in range(1, N + 1):
    if i % 3 == 0 or "3" in str(i):
        print(" {}".format(i), end="")
print()

# ITP1_6_A:

答え
1
2
3
4
_ = int(input())
lst = list(map(int, input().split()))
lst.reverse()
print(*lst)

# ITP1_6_B:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
N = int(input())
suits = ["S", "H", "C", "D"]
cards = []
for _ in range(N):
    s, n = input().split()
    if s == "S":
        cards.append(int(n))
    elif s == "H":
        cards.append(13 + int(n))
    elif s == "C":
        cards.append(26 + int(n))
    else:
        cards.append(39 + int(n))

for i in range(1, 53):
    if i not in cards:
        print(suits[(i - 1) // 13], (i - 1) % 13 + 1)

ele in lstn = len(lst)として$O(n)$らしい.

# ITP1_6_C:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
N = int(input())
room = [[[0] * 10 for _ in range(3)] for _ in range(4)]

for _ in range(N):
    b, f, r, v = map(int, input().split())
    room[b - 1][f - 1][r - 1] += v

for i in range(4):
    for j in range(3):
        for k in range(10):
            print(" {}".format(room[i][j][k]), end="")
        print()
    if i != 3:
        print("####################")

# ITP1_6_D:

答え
1
2
3
4
5
6
7
8
9
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
B = [int(input()) for _ in range(M)]

for i in range(N):
    ans = 0
    for j in range(M):
        ans += A[i][j] * B[j]
    print(ans)

# ITP1_7_A:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
while True:
    m, f, r = map(int, input().split())
    if m == -1 and f == -1 and r == -1:
        break
    score = m + f
    if m == -1 or f == -1:
        print("F")
    elif 80 <= score:
        print("A")
    elif 65 <= score:
        print("B")
    elif 50 <= score:
        print("C")
    elif 30 <= score:
        if 50 <= r:
            print("C")
        else:
            print("D")
    else:
        print("F")

# ITP1_7_B:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
while True:
    N, X = map(int, input().split())
    if N == 0 and X == 0:
        break
    cnt = 0
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            if j < X - i - j <= N:
                cnt += 1
    print(cnt)

# ITP1_7_C:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
H, W = map(int, input().split())
sheet = [list(map(int, input().split())) for _ in range(H)]
for h in range(H):
    sheet[h].append(sum(sheet[h]))
sheet.append([0 for _ in range(W + 1)])
for w in range(W + 1):
    for h in range(H):
        sheet[-1][w] += sheet[h][w]
for r in sheet:
    print(*r)

# ITP1_7_D:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
N, M, L = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
B = [list(map(int, input().split())) for _ in range(M)]

C = [[0 for _ in range(L)] for _ in range(N)]
for i in range(N):
    for j in range(L):
        c = 0
        for k in range(M):
            c += A[i][k] * B[k][j]
        C[i][j] = c
for r in C:
    print(*r)

# ITP1_8_A:

答え
1
2
string = input()
print(string.swapcase())

# ITP1_8_B:

答え
1
2
3
4
5
6
7
8
9
while True:
    x = int(input())
    if x == 0:
        break
    ans = 0
    while x:
        ans += x % 10
        x //= 10
    print(ans)

# ITP1_8_C:

答え
1
2
3
4
5
6
7
8
9
import sys
string = sys.stdin.read().lower()
cnt = [0] * 26
alphabets = [chr(ord('a') + i) for i in range(26)]
for x in string:
    if x in alphabets:
        cnt[alphabets.index(x)] += 1
for i in range(26):
    print("{} : {}".format(alphabets[i], cnt[i]))

# ITP1_8_D:

答え
1
2
3
4
5
6
7
s = input()
p = input()
s = s + s
if s.find(p) != -1:
    print("Yes")
else:
    print("No")

# ITP1_9_A:

答え
1
2
3
4
import sys
word = input()
text = sys.stdin.read()
print(text.lower().split().count(word))

# ITP1_9_B:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
while True:
    cards = input()
    if cards == "-":
        break
    m = int(input())
    for i in range(m):
        interval = int(input())
        head = cards[:interval]
        tail = cards[interval:]
        cards = tail + head
    print(cards)

# ITP1_9_C:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
N = int(input())
taro = 0
hanako = 0
for i in range(N):
    card_taro, card_hanako = input().split()
    if card_taro == card_hanako:
        taro += 1
        hanako += 1
    elif card_taro < card_hanako:
        hanako += 3
    else:
        taro += 3
print(taro, hanako)

# ITP1_9_D:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
text = input()
N = int(input())
for _ in range(N):
    command = input().split()
    if command[0] == "replace":
        a = int(command[1])
        b = int(command[2])
        s = command[3]
        text = text[:a] + s + text[b + 1:]
    elif command[0] == "reverse":
        a = int(command[1])
        b = int(command[2])
        text = text[:a] + "".join(reversed(text[a:b + 1])) + text[b + 1:]
    else:
        a = int(command[1])
        b = int(command[2])
        print(text[a:b + 1])

# ITP1_10_A:

答え
1
2
x1, y1, x2, y2 = map(float, input().split())
print(((x1 - x2) **2 + (y1 - y2) ** 2) ** 0.5)

# ITP1_10_B:

答え
1
2
3
4
5
6
7
8
import math
a, b, C=map(float, input().split())
theta = math.radians(C)
h = b * math.sin(theta)
S = (a * h) / 2
c = math.sqrt(a ** 2 + b ** 2 - 2 * a * b * math.cos(theta))
L = a + b + c
print(S, L, h, sep="\n")

# ITP1_10_C:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
while True:
    N = int(input())
    if N == 0:
        break
    score = list(map(int, input().split()))
    mean = sum(score) / N
    variant = 0
    for i in range(N):
        variant += (score[i] - mean) ** 2
    print((variant / N) ** 0.5)

# ITP1_10_D:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def dist(xs, ys, p):
    ret = 0
    for (x, y) in zip(xs, ys):
        ret += abs(x - y) ** p
    return ret ** (1 / p)

N = int(input())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
for p in range(1, 4):
    print(dist(xs, ys, p))
print(max(abs(x - y) for (x, y) in zip(xs, ys)))

# ITP1_11_A:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Dice:
    def __init__(self, nums):
        self.s1 = nums[0]
        self.s2 = nums[1]
        self.s3 = nums[2]
        self.s4 = nums[3]
        self.s5 = nums[4]
        self.s6 = nums[5]
    def rotate(self, dir):
        if dir == "N":
            self.s1, self.s2, self.s5, self.s6 = self.s2, self.s6, self.s1, self.s5
        elif dir == "S":
            self.s1, self.s2, self.s5, self.s6 = self.s5, self.s1, self.s6, self.s2
        elif dir == "E":
            self.s1, self.s3, self.s4, self.s6 = self.s4, self.s1, self.s6, self.s3
        else: # dir == "W":
            self.s1, self.s3, self.s4, self.s6 = self.s3, self.s6, self.s1, self.s4

nums = list(map(int, input().split()))
ops = input()
d = Dice(nums)
for op in ops:
    d.rotate(op)
print(d.s1)

# ITP1_11_B:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Dice:
    def __init__(self, nums):
        self.nums = nums
    def rotate(self, dir):
        if dir == "N":
            self.nums[0], self.nums[1], self.nums[4], self.nums[5] = self.nums[1], self.nums[5], self.nums[0], self.nums[4]
        elif dir == "S":
            self.nums[0], self.nums[1], self.nums[4], self.nums[5] = self.nums[4], self.nums[0], self.nums[5], self.nums[1]
        elif dir == "E":
            self.nums[0], self.nums[2], self.nums[3], self.nums[5] = self.nums[3], self.nums[0], self.nums[5], self.nums[2]
        else: # dir == "W":
            self.nums[0], self.nums[2], self.nums[3], self.nums[5] = self.nums[2], self.nums[5], self.nums[0], self.nums[3]
    def query(self, top, front):
        saved = nums
        ret = -1
        for dir in "NNNNWNNNWNNNENNNENNNWNNN":
            self.rotate(dir)
            if self.nums[0] == top and self.nums[1] == front:
                ret = self.nums[2]
                break
        self.nums = saved
        return ret

nums = list(map(int, input().split()))
N = int(input())
d = Dice(nums)
for _ in range(N):
    top, front = map(int, input().split())
    print(d.query(top, front))

# ITP1_11_C:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Dice:
    def __init__(self, nums):
        self.nums = nums
    def rotate(self, dir):
        if dir == "N":
            self.nums[0], self.nums[1], self.nums[4], self.nums[5] = self.nums[1], self.nums[5], self.nums[0], self.nums[4]
        elif dir == "S":
            self.nums[0], self.nums[1], self.nums[4], self.nums[5] = self.nums[4], self.nums[0], self.nums[5], self.nums[1]
        elif dir == "E":
            self.nums[0], self.nums[2], self.nums[3], self.nums[5] = self.nums[3], self.nums[0], self.nums[5], self.nums[2]
        else: # dir == "W":
            self.nums[0], self.nums[2], self.nums[3], self.nums[5] = self.nums[2], self.nums[5], self.nums[0], self.nums[3]
    def query(self, top, front):
        saved = nums
        ret = -1
        for dir in "NNNNWNNNWNNNENNNENNNWNNN":
            self.rotate(dir)
            if self.nums[0] == top and self.nums[1] == front:
                ret = self.nums[2]
                break
        self.nums = saved
        return ret
    def is_equal(self, other):
        for dir in "NNNNWNNNWNNNENNNENNNWNNN":
            self.rotate(dir)
            flag = True
            for k in range(6):
                if self.nums[k] != other.nums[k]:
                    flag = False
                    break
            if flag:
                return True
        return False


nums = list(map(int, input().split()))
d1 = Dice(nums)
nums = list(map(int, input().split()))
d2 = Dice(nums)
if d1.is_equal(d2):
    print("Yes")
else:
    print("No")

# ITP1_11_D:

答え
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Dice:
    def __init__(self, nums):
        self.nums = nums
    def rotate(self, dir):
        if dir == "N":
            self.nums[0], self.nums[1], self.nums[4], self.nums[5] = self.nums[1], self.nums[5], self.nums[0], self.nums[4]
        elif dir == "S":
            self.nums[0], self.nums[1], self.nums[4], self.nums[5] = self.nums[4], self.nums[0], self.nums[5], self.nums[1]
        elif dir == "E":
            self.nums[0], self.nums[2], self.nums[3], self.nums[5] = self.nums[3], self.nums[0], self.nums[5], self.nums[2]
        else: # dir == "W":
            self.nums[0], self.nums[2], self.nums[3], self.nums[5] = self.nums[2], self.nums[5], self.nums[0], self.nums[3]
    def query(self, top, front):
        saved = nums
        ret = -1
        for dir in "NNNNWNNNWNNNENNNENNNWNNN":
            self.rotate(dir)
            if self.nums[0] == top and self.nums[1] == front:
                ret = self.nums[2]
                break
        self.nums = saved
        return ret
    def is_equal(self, other):
        for dir in "NNNNWNNNWNNNENNNENNNWNNN":
            self.rotate(dir)
            flag = True
            for k in range(6):
                if self.nums[k] != other.nums[k]:
                    flag = False
                    break
            if flag:
                return True
        return False

N = int(input())
dices = []
for _ in range(N):
    nums = list(map(int, input().split()))
    dices.append(Dice(nums))

flag = True
for i in range(N - 1):
    for j in range(i + 1, N):
        if dices[i].is_equal(dices[j]):
            flag = False
            break
if flag:
    print("Yes")
else:
    print("No")
Hugo で構築されています。
テーマ StackJimmy によって設計されています。