2
1

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 3 years have passed since last update.

【AOJ】Introduction to Programming I をPython3で解説(前編)

Last updated at Posted at 2021-05-19

AOJ(Aizu Online Judge)、Introduction to Programming Iの解答例。

今回は、前編(1_A - 5_D)までの解答例。

1_A:Hello World

print('Hello World')

1_B:X Cubic

x = int(input())
print(x**3)

1_C:Rectangle

A,B = map(int, input().split())
print(A*B, 2*(A+B))

1_D:Watch

S = int(input())

h = int(S / 3600)
m = int((S % 3600) / 60)
s = int(S % 3600 % 60)
print('{}:{}:{}'.format(h, m, s))

2_A:Small, Large, or Equal

a,b = map(int, input().split())

if a < b:
    print('a < b')
elif a > b:
    print('a > b')
else:
    print('a == b')

2_B:Range

a,b,c = map(int, input().split())

if a < b < c:
    print('Yes')
else:
    print('No')

2_C:Sorting Three Numbers

l = list(map(int, input().split()))
l.sort()
print(l[0], l[1], l[2])

2_D:Circle in a Rectangle

W, H, x, y, r = map(int, input().split())

if x-r<0 or x+r>W:
    print('No')
elif y-r<0 or y+r>H:
    print('No')
else:
    print('Yes')

3_A:Print Many Hello World

for _ in range(1000):
    print('Hello World')

3_B:Print Test Cases

cnt = 1
while True:
    x = int(input())
    if x == 0:
        break
    print('Case {}: {}'.format(cnt, x))
    cnt += 1

3_C:Swapping Two Numbers

while True:
    x, y = map(int, input().split())
    if x == 0 and y == 0:
        break
    elif x > y:
        x, y = y, x
    print(x, y, sep=' ')

3_D:How Many Divisors?

for ver.

a, b, c = map(int, input().split())
cnt = 0

for i in range(a, b+1):
    if c % i == 0:
        cnt += 1

print(cnt)

while ver.

a, b, c = map(int, input().split())
cnt = 0
num = a

while True:
    if c % num == 0:
        cnt += 1
    num += 1
    if num > b:
        break

print(cnt)

4_A:A / B Problem

a, b = map(int, input().split())

d = a//b
r = a%b
f = a/b

# format( ,'.f')で小数点調節
print(d, r, format(f, '.5f'), sep=' ')

4_B:Circle

pi = 3.14159265358979
r = float(input())

S = pi*(r**2)
C = 2*pi*r

print(format(S, '.6f'), format(C, '.6f'), sep=' ')

4_C:Simple Calculator

while True:
    a, op, b = input().split()
    a, b = int(a), int(b)

    if op == '?':
        break
    elif op == '+':
        print(a+b)
    elif op == '-':
        print(a-b)
    elif op == '*':
        print(a*b)
    elif op == '/':
        # 小数点以下を切り捨て
        print(a//b)

4_D:Min, Max and Sum

関数なし ver.

# 関数なし
n = int(input())
A = list(map(int, input().split()))

max_a = -1000001
min_a = 1000001
total = 0

for a in A:
    if a > max_a:
        max_a = a
    if a < min_a:
        min_a = a
    total += a

print(min_a, max_a, total, sep=' ')

関数あり ver.

# min(), max(), sum()
n = int(input())
a = list(map(int, input().split()))

print(min(a), max(a), sum(a), sep=' ')

5_A:Print a Rectangle

while True:
    H, W = map(int, input().split())
    if H == 0 and W == 0:
        break
    for _ in range(H):
        print('#'*W)
    print()

5_B:Print a Frame

二重ループ ver.

# 二重ループ
while True:
    H, W = map(int, input().split())
    if H == 0 and W == 0:
        break
    
    for i in range(H):
        for j in range(W):
            if i == 0 or i == H-1 or j == 0 or j == W-1:
                print('#', end='')
            else:
                print('.', end='')
        print()
        
    print()

whileだけ ver.

# while
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), '#', sep='')
    print('#'*W)
    print()

5_C:Print a Chessboard

while True:
    H, W = map(int, input().split())
    if H == 0 and W == 0:
        break
    for i in range(H):
        for j in range(W):
            if (i+j) % 2 == 0:
                print('#', end='')
            else:
                print('.', end='')
        print()
    print()

5_D:Structured Programming

1桁ずつ落とす ver.

n = int(input())
three = []

for i in range(3, n+1):
    if i % 3 == 0:
        three.append(i)
    else:
        test = i
        for j in range(5):
            x = test % 10
            if x == 3:
                three.append(i)
                break
            test //= 10

print(*three)

'3' in str() ver.

# '3' in str(i)
n = int(input())

for i in range(1, n+1):
    if i%3 == 0 or '3' in str(i):
        print(' {}'.format(i), end='')
print()
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?