목록algorithm/C++ (19)
on my way

* 생각한 아이디어 및 문제 풀이 삼각형의 조건은 가장 큰 변의 길이 > N; for(int i=0; i> input; v.push_back(input); } sort(v.begin(), v.end(), greater()); int i=0;..

* 생각한 아이디어 및 문제 풀이 문제 자체가 merge 정렬의 과정이라고 생각했다. merge 정렬을 하는 과정에서 마지막으로 합치는 과정을 생략하고 출력하면 되겠다고 생각했다. * 코드 #include using namespace std; #define MAX_SIZE 1048576 int sorted[MAX_SIZE]; int n, person; void merge(int list[], int left, int mid, int right){ if( (right-left) > (n/person) ) return; int i, j, k; i = left; j = mid+1; k = 0; /* 분할 정렬된 list의 합병 */ while(i

* 생각한 아이디어 및 문제 풀이 입력받은 수에 0이 없으면 -1을 출력한다. 수를 정수로 입력받으면 각 값을 받고 벡터를 넣을 때 나머지나 몫을 구해야할 것 같아서 그냥 문자열로 받아야 된다고 생각했다. 출력값은 내림차순으로 정렬한다. 그러나 도저히 아이디어가 떠오르지 않아서 찾아보니, 30의 배수가 되는 조건은 반드시 끝자리가 0이고, 모든 자리의 수를 더하면 3배수라는 것을 알게 되었다. * 코드 #include #include #include #include using namespace std; int main(int argc, char const *argv[]){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string N; long..

* 생각한 아이디어 및 문제 풀이 메모리가 관건이라고 생각을 했다. 단순히 크게 배열을 선언하는 것 보다 벡터를 선언해서 들어오는 수마다 push를 하는게 효율적이라고 생각했다. 그리고 algorithm의 sort로 내림차순을 지정하면 될 것 같다고 생각했다. * 코드 #include #include #include using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N, input; vector v; cin >> N; for(int i=0; i> input; v.push_back(input); } sort(v.begin(), v.end(), greater()); for(auto& i..

* 문제를 풀기 위해 생각한 아이디어 크기가 3인 배열을 선언하고, 차례대로 컵번호 {1,2,3}으로 선언한다. swap함수로 해당 위치의 컵 바꾸기를 진행하고 최종적으로 1번컵이 위치한 배열의 순서를 출력한다. * 코드 #include #include using namespace std; int main(int argc, char const *argv[]){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int A, B, M; int arr[3]={1, 2, 3}; cin >> M; for(int i=0; i> A >> B; swap(arr[A-1], arr[B-1]); } for(int i=0; i

* 문제를 풀기 위해 생각한 아이디어 벡터를 선언하고, reverse함수를 통해 해당 구간을 뒤집는다. * 코드 #include #include #include using namespace std; int main(int argc, char const *argv[]){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int start, end; vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; for(int i=0; i> start >> end; reverse(v.begin()+start-1, v.begin()+end); } for(int i=0; i