본문 바로가기

전체 글

(80)
C++ 1 ~ N 까지의 약수 개수 (1과 자기자신 포함) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include using namespace std; int cnt[50001]; int main(){ int i, j, n; scanf("%d", &n); for (i = 1; i
Python 순열(Permutation) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # 1 ~ n 까지의 숫자가 있을 때 나열할 수 있는 경우의 수 혹은 경우 def perm(a): length = len(a) if length == 1: return [a] else: result = [] for i in a: b = a.copy() b.remove(i) b.sort() for j in perm(b): j.insert(0, i) if j not in result: result.append(j) return result if __name__ == "__main__": num = int(input('1부터 n까지 자연수를 나열하는 순열을 구합니다. n 을 입력하세요 : ')) a..
Python itertools를 이용한 순열과 조합 (combinations, permutations) 1 2 3 4 5 6 7 8 9 10 11 12 13 import itertools abc = [1,2,3,4] #abc = ['a','b','c','d'] b = itertools.permutations(abc, 3) #target, length(Non-Essential) for i in b: #i = list(i) print(list(i)) b = itertools.combinations(abc, 2) #target, length(Essential) for i in b: #i = list(i) print(list(i))
Python String copy 1 2 3 4 5 6 7 8 9 # variable a and b id equal a = "python" b = a print(id(a)) print(id(b),"\n") b = (a+".")[:-1] # python string slice print(id(a)) print(id(b))
[백준] 10773 제로 Python C++ 1 2 3 4 5 6 7 8 9 n = int(input()) result = [] for i in range(n): inputs = int(input()) if inputs == 0: result.pop() else: result.append(inputs) print(sum(result)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include #include #include #include #include #include #include #include #include using namespace std; int main(..
[백준] 11047 동전0 Python C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 n, k = map(int, input().strip().split()) inputs = [] cnt = 0 point = 0 temp = 0 for i in range(n): temp = int(input()) inputs.append(temp) if temp > k; int *inputs = new int[n]; for (int i = 0; i > *(inputs + i); if (k >= *(inputs + i)) point = i; //cout
[백준] 2581 소수 Python (주어진 범위 내 소수들의 합, 최소값) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 m = int(input()) n = int(input()) lists = [] for i in range(m, n+1): check = 0 if i >= 2: if i == 2: lists.append(i) else: for j in range(2, i): if i%j == 0: check = 1 break if check == 0: lists.append(i) if len(lists) >= 1: print(sum(lists)) print(min(lists)) else: print(-1)
[백준] 소수찾기 1978 Python C++ Python 1234567891011121314151617tc = int(input())inputs = list(map(int, input().strip().split()))cnt = 0 for i in inputs: nop = 0 if i>=2: if i == 2: cnt +=1 else: for j in range(2,i): if i%j == 0: nop = 1 break if nop == 0: cnt +=1print(cnt)Colored by Color Scriptercs C++ 1234567891011121314151617181920212223242526272829303132333435#include using namespace std; int main(){ int n; int tc; cin >>..