STUDY/Algorithm

[python] sw expert academy

wonpick 2021. 5. 10. 12:34

1. 2072. 홀수만 더하기

T = int(input())
for i in range (T):
    number = list(map(int, input().split()))
    odd_num = [num for num in number if num % 2 == 1]
    print("#{} {}".format(i+1,sum(odd_num)))

2. 2070. 큰 놈, 작은 놈, 같은 놈

T = int(input())
for i in range (T):
    a,b = map(int, input().split())
    if a == b:
        print("#{} {}".format(i+1,"="))
    elif a > b:
         print("#{} {}".format(i+1,">"))
    else:
         print("#{} {}".format(i+1,"<"))    

3. 2056. 연월일 달력

T = int(input()) 
for i in range(T): 
    numbers = input()
    date=int(numbers[6:])
    month=int(numbers[4:6])
    year=int(numbers[0:4])
    if month <1 or month>12:
        print('#{} {}'.format(i+1,-1))
        continue

    if month in [1,3,5,7,8,10,12]:
        if  date <1 or  date>31:
            print('#{} {}'.format(i+1,-1))
            continue
    if month ==2:
        if  date<1 or  date>28:
            print('#{} {}'.format(i+1,-1))
            continue

    if month in [4,6,9,11]:
        if  date <1 or  date>30:
            print('#{} {}'.format(i+1,-1))
            continue
    print("#%d %04d/%02d/%02d" %(i+1,year,month, date))

4. 1986. 지그재그 숫자

n = int(input())
for i in range (n):
    k = 0
    a = int(input())
    for p in range (a):
        if (p+1)%2 == 0:
            t=(p+1)*-1
        else:
            t=(p+1)
        k =k+t
    print(f"#{i+1} {k}")

2번째 방법

a = int(input())
for i in range (1,a+1):
    n = int(input())
    b=0
    for nn in range (1,n+1):
        if nn%2==1:
            b+=nn
        else:
            b-=nn
    print('#{} {}'.format(i, b))

2021.파리퇴치

사각형의 크기를 b라고하고, 파리채의 크기를 c라고 했을 때
테스트 할 수 있는 횟수는 가로(b-c+1), 세로(b-c+1)이다.

a = int(input())
for num in range (1,a+1):
    b,c = map(int,input().split())
    arr = [list(map(int,input().split())) for _ in range(b)]
    max = 0
    for i in range (b-c+1):
        for j in range(b-c+1):
            current = 0
            for k in range (c):
                for m in  range (c):
                    current +=arr[i+k][j+m]
                if current>max:
                    max = current
    print("#{} {}".format(num,max))

1859. 백만장자 되기 프로젝트백만장자 되기 프로젝트


처음에는 생각을 리스트로 만들어서 max값의 index를 구한 뒤 그 index값보다 작은 index 값을 가지고 있는 애들을 찾아 연산을 하면된다고 생각했다.
그런데 그 이후에 1, 1, 3, 1, 2의 경우와 같이 제일 큰값 뒤에 전날보다 큰값이 생기게 되면 오류가 난다는 것을 알 수 있었다. 그래서 max값의 index를 제외하고 다시 리스트를 만들어서 하려고 했는데 코드가 너무 복잡해져 아래와 같이 리스트를 뒤집어서 하는걸 참고해서 해결... 😨

a = int(input())
for i in range(1,a+1):
    b = int(input())
    w = list(map(int,input().split()))
    w=w[::-1]
    m=w[0]
    result=0
    for j in range(len(w)):
        if m>w[j]:
            result+= m -w[j]
        else:
            m=w[j]

    print("#{} {}".format(i,result))

 잘못된 코드
#a = int(input())
# for i in range(1,a+1):
#     b = int(input())
#     w = list(map(int,input().split()))
#     index  = w.index(max(w))
#     result = 0
#     for j in range(b):
#         if (w.index(w[j]))<index:
#             result+=max(w)-w[j]
#     print("#{} {}".format(i,result))