목록C, C++ 문제풀이 (39)
rueki

#include using namespace std; int gcd(int a, int b) { if (b == 0)return a; else return gcd(b, a % b); } int main() { int n,t; cin >> t; for (int i = 0; i > n; for (int j = 0; j > arr[j]; } long long sum = 0; for (int y = 0; y < n; y++) { for (int x = y; x < n; x++) { sum += gcd(arr[y], arr[x]); } } cout

GCD 유클리드 호제법 사용(재귀함수) a,b 두 수가 있을 때 b가 0이 될때까지 a를 b로 나눈다. b가 0이되면 a를 return 하게 된다. LCD 최소공배수 = 최대공약수 * (a / 최대공약수) * (b/최대공약수) #include using namespace std; int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int lcm(int g, int a, int b) { int l = g * (a / g) * (b / g); return l; } int main() { int a, b; cin >> a >> b; cout
#include #include using namespace std; int cnt=0; int DFS(int L, int s, vector num,int t) { // 원소 개수 int n = num.size(); // 이진트리 깊이 == 원소개수 -> 재귀 멈추는 조건 if(L==n) { // total sum이 타겟값과 같다면? if(s == t) { // 부분집합에 대한 갯수를 세는 것이기에 cnt++ cnt++; } } else{ //종료조건이 안되면 계속 재귀 호출 DFS(L+1, s+num[L], num,t); DFS(L+1, s-num[L],num,t); } return cnt; } int solution(vector numbers, int target) { int answer = DFS(..
이진트리 구조 typedef struct{ int data; //노드 데이터 struct Node *leftChild; struct Node *rightChild; }Node; Node* initNode(int data, Node* leftChild, Node* rightChild){ Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->leftChild = leftChild; node->rightChild = rightChild; return node; } 전위 순회 자기 자신 - 왼쪽 노드 - 오른쪽 노드 void preorder(Node* root){ if(root){ printf("%d",root->data); preorder(ro..