전체 글 (454) 썸네일형 리스트형 [Level 2][C++] 예상 대진표 #include using namespace std;int giveNum(int a){ if(a%2==0) return a/2; else return a/2+1;}int tornumant(int a, int b){ if(a==b) return 0; a = giveNum(a); b = giveNum(b); return 1+tornumant(a, b); //토너먼트 치룰 때 마다 1씩 증가. }int solution(int n, int a, int b){ return tornumant(a,b);} 반복문으로 2를 계속 나눠서 같은 숫자가 나올 때까지 카운트 해도 되는데,재귀함수를 활용해보고 싶어서 재귀함수로 풀었다. [Level 2][C++] 소수 만들기 #include #include using namespace std;int solution(vector nums) { vector odds; vector evensOne; vector evensTwo; //짝짝 집어넣는 곳 vector addThree; //짝짝홀, 홀홀홀 더한 애들을 집어 넣는 곳. //홀수 짝수 분류 for (int i = 0; i 1) { for (int i = 0; i 2) { for (int i = 0; i 짝수 홀수 안나누고, 그냥 반복문 3개 겹쳐서 전부 3개씩 더한 다음에 알아내는 방법도 있는데효율성이 안좋을 거 같아 나눠서 계산했다. [Level 2][C++] 점프와 순간 이동 #include using namespace std;int solution(int n){ int ans=0; while(n>0) { ans += n%2; n/=2; } return ans;} 2를 곱한 위치 만큼 간다? => 목표 하는 숫자를 2로 계속 나눈다. 그리고 나눌때마나 나오는 나머지 만큼 이동한다. 이전 1 ··· 6 7 8 9 10 11 12 ··· 152 다음