분류 전체보기104 [알고리즘] C++ 스택, 큐, 덱 스택 - 한 쪽 끝에서만 원소를 넣고 빼는 구조 - LIFO(Last In First Out): 나중에 들어온 것이 먼저 나감 - 헤더: #include - 선언: stack이름 기본 함수 - push(a): stack에 a 추가 - pop(): 제일 마지막에 추가된 원소 제거 - top(): 제일 마지막에 추가된 원소 반환 - empty(): stack이 비어있으면 true, 아니면 false 반환 - size(): stack에 들어있는 원소의 개수 반환 #include #include using namespace std; stacks; int main(){ s.push(1);//s={1} s.push(2);//s={1,2} s.push(3);//s={1,2,3} s.pop();//s={1,2} cout 2021. 9. 8. [C++] 백준 6764번: Sounds fishy! 오타가 있는 문제입니다. 네 수가 모두 같을 때는 "Fish at Constant Depth"라고 출력해야 하고 적혀진 세 조건을 만족하지 못했을 때는 "No Fish"라고 출력해야 합니다.(No Fish에 . 을 빼야함) #include #include using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int arr[4]; int up = 0; int down = 0; int same = 0; for (int i = 0; i > arr[i]; } for (int i = 1; i arr[i - 1]) up++; else i.. 2021. 3. 13. [C++] 백준 10799번: 쇠막대기 ')'가 입력될 때 이전 괄호가 '('이면 현재까지 저장된 '(' 개수를 더해주고 이전 괄호가 ')'라면 1을 더해준다. #include #include #include using namespace std; stack s; int main() { string str; cin >> str; int res = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == '(') { s.push(str[i]); } else { s.pop(); if (str[i - 1] == '(') { res += s.size(); } else { res ++; } } } cout 2021. 2. 4. [C++] 백준 16206번: 롤케이크 1. 10의 배수를 먼저 크기 순서대로 정렬한 후 나머지 숫자는 뒤에 붙인다. 2. 배열을 돌면서 10보다 크고 m>0일 동안 v[i]에서 10을 빼면서 카운트를 센다. 3. v[i]가 10인 경우를 따로 카운트 해준다. #include #include #include using namespace std; vectorv; vectorv2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, a; cin >> n >> m; for (int i = 0; i > a; if (a % 10 == 0) v.push_back(a); else v2.push_back(a); } sort(v.begin().. 2021. 1. 23. [C++] 백준 2110번: 공유기 설치 #include #include #include using namespace std; vectorv; int main(){ int n, c; cin >> n >> c; for (int i = 0; i > a; v.push_back(a); } sort(v.begin(), v.end()); int left = 1; int right = v[n-1]-v[0]; int result = 0; while (left = mid) { cnt++; loc = v[i]; } } if (cnt >= c) { left = mid + 1; result = mid; } else right = mid - 1; } cout 2020. 12. 30. [C++] 백준 10870번: 피보나치 수 5 1. 배열을 선언하고 arr[0]=0, arr[1]=1을 저장 2. 2부터 시작해서 n이 될때까지 피보나치 식을 써서 값을 구하고 배열에 저장 #include using namespace std; int arr[21]; int i = 2; int fibo(int n) { while (i > n; arr[0] = 0; arr[1] = 1; fibo(n); cout 2020. 12. 27. 이전 1 ··· 12 13 14 15 16 17 18 다음