Algorithm/백준과 프로그래머스 26

[C++] 9012. 괄호

풀이과정 괄호 문제는 스택의 대표적 문제이다. 짝맞추는 문제이므로 '('가 들어왔을때만 스택에 넣어준다. ')'가 들어왔을 때 스택이 비어있다면 잘못된 괄호이므로 "NO"를 출력해주고 아니라면 짝이 맞았으므로 스택을 pop해준다. 문자열 길이만큼 진행한 뒤 스택이 비어있지 않다면 짝이 안맞았음을 의미하므로 "NO"를 출력한다. 스택이 비어있다면 짝이 다 맞았다는 것을 의미하므로 "YES"를 출력한다. #include #include using namespace std; void VPS(string inp) { stack s; for (int i = 0; i < inp.length(); i++) { if (inp[i] == '(') s.push('('); else { if (!s.empty()) s.pop..

[C++] 10828. 스택

간단하게 스택을 구현하면 되는 문제로, 배열을 써서 풀었고 STL stack을 사용한 간단한 버전도 올려놨다. [1차원 배열 이용] #include using namespace std; void push(int value); void pop(); void size(); void empty(); void top(); int N; int top_v = -1; int myStack[100000]; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string com; cin >> N; for(int i=0; i> com; if(com == "push") { int x; cin >> x; push(x); } else if(c..

[C++] 2751. 수 정렬하기 2

단순한 sorting 문제로, 직접 구현해도 되지만 나는 간단하게 STL의 sort 함수를 이용했다. #include #include using namespace std; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; int arr[N]; for(int i=0; i> arr[i]; sort(arr, arr+N); for(auto x : arr) cout a; arr.push_back(a); } sort(arr.begin(), arr.end()); arr.erase(std::unique(arr.begin(), arr.end()), arr.end()); for(auto x : arr) c..

[C++] 5576번 콘테스트

각각 10개의 입력으로 정해져 있으므로 크기 10짜리 배열 2개를 만들어 입력을 받아주고 sort함수를 이용하여 내림차순으로 정렬을 해주었다. 높은 점수 순으로 3명의 점수만 합산되므로 3번째까지의 점수를 각각 더해주어서 출력해주었다. #include #include #define sync ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); using namespace std; void Contest(){ int W[10], K[10]; int W_score = 0; int K_score = 0; for(int i=0; i> inp; W[i] = inp; } sort(W, W+10, greater()); for(int i=0; i> inp; K[i]..

728x90