전체 글

전체 글

    분해합 풀이

    문제 링크: https://www.acmicpc.net/problem/2231 백준 브루트 포스 2단계 - 2231번 분해합을 풀어보았다. 풀이: 분해합을 1부터 n까지 구해 비교한다. 파이썬의 경우, sum(map(int,str(i)))로 자릿수의 합을 구할 수 있다는 것을 알게 되었다. C++ #include #include using namespace std; int main() { int n; cin>>n; for(int i=1;i0) { check+=ii%10; ii/=10; } if(check==n) { cout

    블랙잭 풀이

    문제 링크: https://www.acmicpc.net/problem/2798 백준 브루트 포스 1단계 - 2798번 블랙잭을 풀어보았다. 풀이: 카드 세 장을 골라 그 합이 입력값과 최대한 비슷하게 만들면 된다. C++ #include #include using namespace std; int main() { int n,m,inp; cin>>n>>m; int mcheck=0; vector v; for(int i=0;i>inp; v.push_back(inp); } for(int i=0;i

    하노이 탑 이동 순서 풀이

    문제 링크: https://www.acmicpc.net/problem/11729 백준 재귀 6단계 - 11729번 하노이 탑 이동 순서를 풀어보았다. 풀이: 하노이 탑을 재귀함수로 작성하면 된다. C++ #include #include using namespace std; void hanoi(int k,int a, int b, int c) { if(k==1) printf("%d %d\n",a,b); else { hanoi(k-1,a,c,b); printf("%d %d\n",a,b); hanoi(k-1,c,b,a); } } int main() { int n; scanf("%d",&n); cout

    별 찍기 - 10 풀이

    문제 링크: https://www.acmicpc.net/problem/2447 백준 재귀 5단계 - 2447번 별 찍기 - 10를 풀어보았다. 풀이: 별 무늬를 찍는 재귀함수를 만들면 된다. 파이썬의 경우, 입출력 시간을 빠르게 하기 위해 input과 print를 바꾸었다. C++ #include using namespace std; void sol(int a,int b, int c) { if((a/c)%3==1&&(b/c)%3==1) printf(" "); else { if(c/3==0) printf("*"); else sol(a,b,c/3); } } int main() { int n; scanf("%d",&n); for(int i=0;i

    알고리즘 수업 - 병합 정렬 1 풀이

    문제 링크: https://www.acmicpc.net/problem/24060 백준 재귀 4단계 - 24060번 알고리즘 수업 - 병합 정렬 1을 풀어보았다. 풀이: 병합 정렬을 구현하고, k번째를 출력하면 된다. C++의 경우, https://codnote.tistory.com/3 를 참고했다. 파이썬의 경우, https://velog.io/@wngud4950/%EB%B0%B1%EC%A4%80-24060.-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EC%88%98%EC%97%85-%EB%B3%91%ED%95%A9%EC%A0%95%EB%A0%AC1 를 참고했다. C++ #include using namespace std; int* A; int* tmp; int N, cnt = ..

    재귀의 귀재 풀이

    문제 링크: https://www.acmicpc.net/problem/25501 백준 재귀 3단계 - 25501번 재귀의 귀재를 풀어보았다. 풀이: 재귀함수를 받아 호출 횟수를 출력하면 된다. 재귀함수에 전역변수를 선언해야 한다. 전역변수를 recursion안에 넣어야 한다. C++ #include #include #include using namespace std; int cnt=0; int recursion(const string& s, int l, int r) { cnt++; if (l >= r) return 1; else if (s[l] != s[r]) return 0; else return recursion(s, l + 1, r - 1); } int isPalindrome(const string..

    피보나치 수 5 풀이

    문제 링크: https://www.acmicpc.net/problem/10870 백준 재귀 2단계 - 10870번 피보나치 수 5를 풀어보았다. 풀이: 피보나치 수를 구현한다. C++ #include using namespace std; int sol(int a) { if(a==0) return 0; if(a==1) return 1; return sol(a-1)+sol(a-2); } int main() { int n; scanf("%d",&n); printf("%d",sol(n)); } Python def fib(a): if a==0: return 0 if a==1: return 1 return fib(a-1)+fib(a-2) n=int(input()) print(fib(n)) Java

    팩토리얼 풀이

    문제 링크: https://www.acmicpc.net/problem/10872 백준 재귀 1단계 - 10872번 팩토리얼를 풀어보았다. 풀이: 재귀함수를 구현하면 된다. 입력값이 0일 때도 신경 쓰자. C++ #include using namespace std; int sol(int a) { if(a==1||a==0) return 1; // 0!의 값은 1 return a*sol(a-1); } int main() { int n; scanf("%d",&n); printf("%d",sol(n)); } Python def fac(a): if a==1 or a==0: return 1 return a*fac(a-1) n=int(input()) print(fac(n)) Java