[C++] 프로그래머스 소수찾기 완전탐색
·
Algorithm/Programmers
#include #include #include using namespace std; int solution(string numbers) { int answer = 0; sort(numbers.rbegin(), numbers.rend()); int num = stoi(numbers); vector visited(num + 1, false); for(int i = 2; i
[C++] 프로그래머스 모의고사 완전탐색
·
Algorithm/Programmers
#include #include #include using namespace std; vector solution(vector answers) { vector answer; int counts[3]{0}; int tester1[5] = {1,2,3,4,5}; int tester2[8] = {2,1,2,3,2,4,2,5}; int tester3[10] = {3,3,1,1,2,2,4,4,5,5}; for(int j = 0; j < answers.size(); j++) { if(tester1[j % 5] == answers[j]) counts[0]++; if(tester2[j % 8] == answers[j]) counts[1]++; if(tester3[j % 10] == answers[j]) counts[2..
[C++] 프로그래머스 네트워크 DFS, BFS
·
Algorithm/Programmers
#include #include #include using namespace std; int visited[200]; void DFS(int curr, int n, vector computers); int solution(int n, vector computers) { int answer = 0; for(int i = 0; i < n; i++) { if (visited[i] == false) { DFS(i, n, computers); answer++; } } return answer; } void DFS(int curr, int n, vector computers) { queue q; q.push(curr); visited[curr] = true; while(!q.empty()) { int currNod..
[C++] 프로그래머스 타겟넘버 DFS/BFS
·
Algorithm/Programmers
#include #include #include using namespace std; int result = 0; void DFS(vector numbers, int target, int sum, int count); int solution(vector numbers, int target) { DFS(numbers, target, 0, 0); int answer = result; return answer; } void DFS(vector numbers, int target, int sum, int count) { if(count == numbers.size()) { if(sum == target) result++; return; } DFS(numbers, target, sum + numbers[count..
[C++] 프로그래머스 H-index
·
Algorithm/Programmers
#include #include #include using namespace std; int solution(vector citations) { int answer = 0; sort(citations.rbegin(), citations.rend()); if (citations[0] == 0) return 0;//0이 최대인경우 for (int i = 0; i < citations.size(); i++) { if (i < citations[i]) answer++;//최대개수에서의 카운트일때까지 증가 else break; } return answer; } i번째가 citations[i]보다 작을경우 하나씩증가시킴.
[C++] 프로그래머스 가장 큰 수
·
Algorithm/Programmers
#include #include #include using namespace std; bool comp(const string &x, const string &y); string solution(vector numbers) { string answer = ""; vector sNumber; for(int i = 0; i < numbers.size(); i++) { sNumber.push_back(to_string(numbers[i])); } sort(sNumber.begin(), sNumber.end(), comp); if(sNumber[0] == "0") return "0"; for(int i = 0; i < numbers.size(); i++) { answer += sNumber[i]; } retur..