코딩/백준 그리디 문제
-
BaekJoon[1339]코딩/백준 그리디 문제 2023. 2. 17. 12:23
이문제는 간단하게 들어온 알파벳의 위치에따라 10을 곱해줘서 우선순위를 리스트에저장하고 ABB#A가 3째자리에있으니 10**(3-1)=100의 우선순위를부여 B는 두번째 첫번쨰자리에있으니 10**(2-1)+10**(1-1)=11 CS#마찬가지로 C=10**(2-1),S=10**(1-1) EX) ['A':100,'B':11,'C':10,'S':1]#정렬 이값에 FOR문을 리버스로 돌려서 9부터 1까지 삽입후 처음에 들어왔던 값들에 매치 시켜주면 끝 alphabet=[input() for i in range(int(input()))] alphabet_value={} priority=[] a=11 for i in alphabet: for j in range(len(i)): if alphabet_value.ge..
-
BaekJoon[16953]코딩/백준 그리디 문제 2023. 2. 17. 12:06
이문제의 핵심은 A를 B로 만드는것이아닌 B를 A로 만드는것이다. 단순히 B가 A가 되기위해서는 맨마지막 숫자1을 빼는게 가장 빠를것이다. 하지만 이는 B 의 맨마지막 숫자가 1일경우에만 가능한 일이고 2를 나누는 경우에는 2로 나우어떨어져야한다. 이는 반복문을 통해 B가A가 될때까지 이루어지고 만약 맨마지막 숫자가 1이 아니고 2로도 나누어 떨어지지 않는다면 이때 -1을 출력 또한 B가 A보다 작을때도 마찬지로 -1을 출력하면된다. import sys input=sys.stdin.readline def calculator(n,m,c): while m>=n: if m==n: print(c) return None if m%10==1: m//=10 elif m%2==0: m//=2 else: print(-1..