문제 https://www.acmicpc.net/problem/15663 15663번: N과 M (9) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net 풀이 지금까지의 N과 M문제들과는 달랐따..푸는데 너무 오래걸림. 중복을 신경써야하는데 시간초과 안나는 방법이 떠오르지 않아서 다른 분들의 풀이를 참고해서 해결했다. 처음에는 DFS로 모든 경우를 만든 뒤 Set을 이용하고 정렬하여 출력해주었따. 누가봐도 시간초과 날 것 같은 코드다. 백트래킹을 이용하려면 중복되는 수가 들어왔을때는 넘어가도록 해야하는데 아이디어가 생각나지 않았다. 그러다 ..
문제 https://www.acmicpc.net/problem/15657 15657번: N과 M (8) N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 www.acmicpc.net 풀이 이전에 비슷한 문제가 있었다. 다른 점은 숫자가 연속된 숫자가 아니라 반복문의 i를 인덱스로 사용해야한다. i로 들어온 numbers[i]의 숫자와 그 이후의 숫자들로만 쌍을 이뤄야하기때문에 파라미터로 i를 num으로 받아 num부터 반복문을 돌렸다. import Foundation let input = readLine()!.split(separator: " ").map { I..
문제 https://www.acmicpc.net/problem/15656 15656번: N과 M (7) N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 www.acmicpc.net 풀이 N과 M(6)보다 오히려 더 간단한 문제였따. 중복이 허용되게 각 숫자별로 모든 경우를 출력해주면 되므로 check같은 변수 필요없이 반복문을 0부터 n까지 돌리면 된다. import Foundation let input = readLine()!.split(separator: " ").map { Int(String($0))! } let numbers = readLine()!.s..
문제 https://www.acmicpc.net/problem/15655 15655번: N과 M (6) N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 www.acmicpc.net 풀이 이 문제에서는 두번째 줄에 주어진 수를 가지고 input[1]만큼의 수를 가지는 문자열을 만들어서 출력하는 것이다. 이때, 선택된 숫자보다 큰 수로만 짝을 이뤄서 출력해야한다. 파라미터로 depth이외에도 시작하는 인덱스를 가지고 있어야한다. 배열을 정렬한 뒤에 해당 인덱스 이후의 숫자들을 체크하면 조건을 만족시킬 수 있기때문이다. import Foundation let inp..
문제 https://www.acmicpc.net/problem/15654 15654번: N과 M (5) N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 www.acmicpc.net 풀이 예제 입출력을 보고 문제의 조건을 찾아내보는게 처음이라 살짝 당황했다. 첫 번째 줄에는 숫자의 개수와 출력할 숫자의 개수를 의미하고, 두번째 줄에서는 숫자가 주어진다. 첫번째 예시를 보면 4,5,2라는 수를 1개씩 순서대로 출력하면 된다는 얘기다. 이번 문제에서는 배열에서 선택된 숫자를 제외한 모든 수를 돌아야하므로 check라는 변수가 필요했다. 방문했는지 체크해야 해당 숫자..
- Total
- Today
- Yesterday