목록Algorithm (187)
:: ADVANCE ::
[Heap Sort] 힙 정렬 힙 정렬은 상당히 효율적인 정렬 알고리즘을 선택 정렬과 유사한 접근 방식을 가졌다. 퀵소트는 대부분의 경우에서 최상위의 성능을 내기 때문에 힙 정렬은 이에 비해 열세이지만,힙소트는 최악의 경우에도 O(NlogN)을 보장한다. 힙 소트는 두가지 단계로 나누어 볼 수 있다. 1. Heap Tree 구성2. 다시 정렬을 하면서 Tree 수정 트리를 수정할 때 가장 상위 노드의 값과 가장 마지막 노드의 값을 교환하여마지막 부터 채워나간다.하지만 이는 알고리즘을 응용하면 처음부터 채워나갈 수 있지 않을까 싶다. 트리와 배열을 그림으로 그려서 표현하고 싶으나 그거까지는 귀찮.... 코드를 구현할 때 참고한 블로그에 그림과 설명이 친절하게 되어있다.이를 참고해서 공부하면 좋을 것 같다...
[Quick Sort] 퀵 정렬 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#include int count; void print(int *data, int size){ int i; for (i = 0; i left) { while (data[left] pivot) { quicksort(data, pivot + 1, end); }} int main(void){ int n, i; int input[100]; scanf("%d", &n); for (i = 0; i
dovelet 9 단계 재귀 특별한 4자리 정수 (sfn) http://59.23.113.171/30stair/sfn/sfn.php?pname=sfn 123456789101112131415161718192021222324252627282930#include int sumofDecimal(int n){ if (!n) return n; return n % 10 + sumofDecimal(n / 10);} int sumofDuodecimal(int n){ if (!n) return n; return n % 12 + sumofDuodecimal(n / 12);} int sumofHexadecimal(int n){ if (!n) return n; return n % 16 + sumofHexadecimal(n / ..
dovelet 9 단계 재귀 유클리드 호제법 (euclid) http://59.23.113.171/30stair/euclid/euclid.php?pname=euclid 12345678910111213141516171819202122#include int gcd(int n, int m) { if (m == 0) return n; else gcd(m, n % m);} int lcm(int n, int m) { int g = gcd(n, m); return n / g * m;} int main(void){ int n, m; scanf("%d %d", &n, &m); printf("%d %d\n", gcd(n, m), lcm(n, m)); return 0;}cs 당연한 얘기겠지만나머지 연산으로 만든 GCD가 마..
dovelet 9 단계 재귀 x의 y 거듭 제곱 (powerofx) http://59.23.113.171/30stair/powerofx/powerofx.php?pname=powerofx 1234567891011121314151617#include int square(int n, int m){ if (m == 1) return n; return n * square(n, m - 1);} int main(void){ int n, m; scanf("%d %d", &n, &m); printf("%d\n", square(n, m)); return 0;}cs 으아아아재귀야 오랜만이다오랜만이라 감을 잃었다...큰일이로다ㅠㅠ
dovelet 옥상 평균값 수열 (coci_prosjek) http://59.23.113.171/pool/coci_prosjek/coci_prosjek.php?pname=coci_prosjek 123456789101112131415161718192021#include int main(void){ int n; int in[2] = { 0, }; int output[110] = { 0, }; scanf("%d", &n); for (int i = 0; i
dovelet 7 단계 다차원 배열 3*3 블럭의 합 (block) http://59.23.113.171/30stair/block/block.php?pname=block 12345678910111213141516171819202122#include int main(void) { int i, j; int output[9] = { 0, }; for (i = 0; i
dovelet 옥상 색종이 (koi_Mpaper) http://59.23.113.171/pool/koi_Mpaper/koi_Mpaper.php?pname=koi_Mpaper 123456789101112131415161718192021222324252627282930313233343536#include int map[110][110];int cnt; int main(void){ int n, i, x, y, j; scanf("%d", &n); while (n--) { scanf("%d%d", &x, &y); for (i = 0; i
dovelet 3 단계 For 최대공약수, 최소공배수 / gcd_lcm http://59.23.113.171/30stair/gcd_lcm/gcd_lcm.php?pname=gcd_lcm 1234567891011121314151617181920212223242526272829303132333435363738394041#include int gcd(int n, int m){ int temp; while (n != m) { if (n > m) { temp = n; n = m; m = temp; } temp = n; n = m - n; m = temp; } return n;} int lcm(int n, int m){ int i = 1, temp = n; while (temp % m != 0) { temp = n ..
ALGOSPOT 문자열 암호화 https://www.algospot.com/judge/problem/read/ENCRYPT 123456789101112131415161718192021#include int main(void){ int t, i; scanf("%d", &t); while (t--) { char input[110] = { 0, }; scanf("%s", input); for (i = 0; input[i]; i += 2) printf("%c", input[i]); for (i = 1; input[i]; i += 2) printf("%c", input[i]); printf("\n"); } return 0;}Colored by Color Scriptercs