본문 바로가기

Algorithm

[C/C++](유클리드 호제법)간단한 최대공약수, 최소공배수(GCD,,LCM) 소스 코드 Code Colored By Color Scripter™1234567891011int gcd(int a, int b){ if (b == 0) return a; gcd(b, a%b);} int lcm(int a, int b){ return (a*b) / gcd(a, b);} 더보기
알고리즘 > 자료구조A > 빌딩(1328) 1. Question 문제 보기 2. Solving 쉬워보여 바로 코딩했다 마지막 Test case에서 Time limit이 걸려버렸다.다양한 풀이법이 있을 수 있지만, Stack을 이용하니 O(n)의 속도가 되어 마지막 Test case도 통과할 수 있었다.input으로 들어오는 빌딩의 높이가 3 2 6 1 1 2 라면,1) 일단 첫번째 수를 Push 3 2) top보다 작거나 같은 수가 나오면 push, 새로운 수가 top보다 큰 수가 나오면 top이 새로 들어올 수보다 클 때까지 pop하고 새로운 수를 push한다. 2가 3보다 작기 때문에 push했다. 2 3 3) 6이 들어올 차례이다. 2가 6보다 작기 때문에 pop, 3도 pop, 그리고 6을 push한다. 6 4) 마찬가지 방법으로...pu.. 더보기
알고리즘 > 자료구조A > 히스토그램(1214) 1. Question 문제 바로가기 2. Solving 3. CodeColored By Color Scripter™1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#include #pragma warning(disable:4996)long long int n, arr[100005];long long int max;long long int minValue();int main(){ FILE* fin = fopen("input.txt", "rt"); FILE* fout = fopen("output.txt", "wt"); fscanf(fin,"%d", &n); //scanf("%d", &n); fo.. 더보기
[Algorithm]이차원 배열 회전 시키기 1. Questionn*n의 이차원 배열의 크기가 가로, 세로 1씩 커지며, 이를 90도 회전 시키는 문제다. 이를 응용하여 대칭도 시킬 수 있다.2. CodeColored By Color Scripter™123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051#include #include #pragma warning(disable:4996) int arr[12][12], tmp[12][12], cnt = 3;void rotate(int des[][12]);int main(){ while (cnt 더보기
[Algorithm]개미(koi_ant) 1. Questionhttp://183.106.113.109/pool/koi_ant/koi_ant.php?pname=koi_ant2. Discussion단순히 개미의 위치를 한칸씩 옮기는 방법으로 일부 테스트 케이스는 통과할 수 있을 것이다. 그러나 계산할 시간 t의 범위가 2,000,000,000개까지 나올 수 있으므로 좀 더 효율적인 방법을 생각해낼 필요가 있다. 3. Code 더보기
[Algorithm]전깃줄 (koi_Ewire) / 29% Question - 전깃줄 (koi_Ewire) http://183.106.113.109/pool/koi_Ewire/koi_Ewire.php?pname=koi_Ewire Explanation 1) 선들과 가장 많이 걸쳐 있는 선부터 삭제한다.2) LCS 알고리즘을 이용한다. 최장 공통 부분 수열 위키피디아Code Coding 더보기
[C/C++]0.5 더하여 반올림하기 Explanation 0.5를 더하면 float가 되고, 값을 int형 변수에 넣어주면 int로 자동 다운 캐스팅되며 반올림이 일어난다. Code Colored By Color Scripter™123456int main(){ int n; n = 0.5 + (float)8 / 3; printf("%d", n);} 더보기
공약수(koi_numM1)/옥상 Question http://183.106.113.109/pool/koi_numM1/koi_numM1.php?pname=koi_numM1 Code Colored By Color Scripter™123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#include #pragma warning(disable:4996) int a, b, ans_a, ans_b;void algo(int *gcd, int *lcm);int gcd1(int ga, int gb); int main(){ scanf("%d %d", &a, &b); algo(&a, &b); return 0;} void algo(int *gcd, int .. 더보기
섞기 수열/koi_orbit (16 %) (coding) 문제 http://183.106.113.109/pool/koi_orbit/koi_orbit.php?pname=koi_orbit 풀이 처음엔 단순 코딩 문제라 생각하고 막 코딩했는데...입력값 n이 100을 넘어가니 time limit이 뜬다.Colored By Color Scripter™1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556#include #include #pragma warning(disable:4996)int n, arr[20005], orbit[20005], sort[20005], temp[20005];int cmp();int main(){ scanf(".. 더보기
[C/C++]버블 정렬(Bubble sort) Code Colored By Color Scripter™123456789101112for (int i = 1; i 더보기