전체 글
암호 만들기 풀이
문제 링크: https://www.acmicpc.net/problem/1759 백준 알고리즘 기초 2/2 530에서 2번째 - 1759번 암호 만들기를 풀어보았다. 풀이: https://it-garden.tistory.com/271 를 참고했다. C++ Python from itertools import combinations l,c=map(int,input().split()) strs=list(input().split()) strs.sort() vowel=set('aeiou') res=list(combinations(strs,l)) for i in res: check=set(i)-vowel c=len(check) if c>=2 and l-c>=1: print("".join(i)) Java
로또 풀이
문제 링크: https://www.acmicpc.net/problem/6603 백준 알고리즘 기초 2/2 520에서 6번째 - 6603번 로또를 풀어보았다. 풀이: 파이썬의 경우 combinations을 이용했다. C++ Python from itertools import combinations while 1: inp=list(map(int,input().split())) n=inp[0] num=inp[1:] if n==0: break for i in combinations(num,6): for j in i: print(j,end=' ') print() print() Java
외판원 순회 2 풀이
문제 링크: https://www.acmicpc.net/problem/10971 백준 알고리즘 기초 2/2 520에서 5번째 - 10971번 외판원 순회 2를 풀어보았다. 풀이: dfs를 쓰는 전형적인 문제이다. C++ Python def dfs(st,cur,weight,cnt): global res if cnt==n: if inp[cur][st]: weight+=inp[cur][st] if res>weight: res=weight return if weight>res: return for i in range(n): if not visit[i] and inp[cur][i]: visit[i]=1 dfs(st,i,weight+inp[cur][i],cnt+1) visit[i]=0 n=int(input()) i..
차이를 최대로 풀이
문제 링크: https://www.acmicpc.net/problem/10819 백준 알고리즘 기초 2/2 520에서 4번째 - 10819번 차이를 최대로를 풀어보았다. 풀이: 브루트 포스 문제인 만큼 전부 구해보면 된다. C++ Python from itertools import permutations n=int(input()) inp=list(map(int,input().split())) per=list(permutations(inp,n)) res=-10000000 for i in per: temp=0 for j in range(n-1): temp+=abs(i[j]-i[j+1]) if temp>res: res=temp print(res) Java
모든 순열 풀이
문제 링크: https://www.acmicpc.net/problem/10974 백준 알고리즘 기초 2/2 520에서 3번째 - 10974번 모든 순열을 풀어보았다. 풀이: 파이썬은 permutation을 썼다. C++ Python from itertools import permutations n=int(input()) arr=[i+1 for i in range(n)] res=permutations(arr) for i in res: print(*i) Java
이전 순열 풀이
문제 링크: https://www.acmicpc.net/problem/10973 백준 알고리즘 기초 2/2 520에서 2번째 - 10973번 이전 순열을 풀어보았다. 풀이: 다음 순열과는 반대의 문제이다. C++ Python n=int(input()) inp=list(map(int,input().split())) for i in range(n-1,0,-1): if inp[i-1]>inp[i]: for j in range(n-1,0,-1): if inp[i-1]>inp[j]: inp[i-1],inp[j]=inp[j],inp[i-1] inp=inp[:i]+sorted(inp[i:],reverse=True) print(*inp) exit(0) print(-1) Java
다음 순열 풀이
문제 링크: https://www.acmicpc.net/problem/10972 백준 알고리즘 기초 2/2 520에서 1번째 - 10972번 다음 순열을 풀어보았다. 풀이: next permutation을 구하는 문제이다. 파이썬은 https://dallae7.tistory.com/155 를 참고했다. C++ Python N=int(input()) arr=list(map(int, input().split())) def next_permutation(): i=N-1 while(i>0 and arr[i-1]>=arr[i]): i-=1 if i==0: return False j=N-1 while(arr[i-1]>=arr[j]): j-=1 arr[i-1], arr[j] = arr[j], arr[i-1] k=N-..
N과 M (12) 풀이
문제 링크: https://www.acmicpc.net/problem/15666 백준 알고리즘 기초 2/2 510에서 12번째 - 15666번 N과 M (12)를 풀어보았다. 풀이: 파이썬의 경우 combinations_with_replacement을 쓰고, set을 써서 중복을 제거했다. C++ Python from itertools import combinations_with_replacement n,m=map(int,input().split()) arr=list(map(int,input().split())) arr.sort() res=list(set(combinations_with_replacement(arr,m))) res.sort() for i in res: for j in i: print(j,..