분류 전체보기 (454) 썸네일형 리스트형 [Level 2][C++] 전화번호 목록 풀이 1 - 해시테이블 이용#include #include using namespace std;bool solution(vector phone_book) {//전화번호 앞자리를 기준으로 해시 테이블에 정리한다. vector> hashT(10); for(int i=0; i 풀이 2 - 정렬해서 푼다.#include #include #include using namespace std; bool solution(vector phone_book) { sort(phone_book.begin(), phone_book.end()); //내림차순으로 정렬 for(int i=0; i [Level 2][C++] H-Index #include #include #include using namespace std;int solution(vector citations) { sort(citations.begin(), citations.end(), greater()); for(int i=0; i citations[i]) return i; if(i+1 == citations[i]) return i+1; } } 맨 끝에서 부터 논문 수를 센다. 논문 수([i+1])>=논문 인용수(citations[i])면 return citations[i]로 문제가 풀릴 줄 알았으나.... 다 틀렸다. 왜 틀렸나 생각해보니 {1,2,3,5,6,7,.. [Level 2][C++] JadenCase 문자열 만들기 #include #include using namespace std;string solution(string s) { int count =1; //공백기준 첫번째 문자 for(int i=0; i '9'){ if(count==1) s[i] = toupper(s[i]); //대문자로 만들어주는 함수 else s[i] = tolower(s[i]); //소문자로 만들어주는 함수 } count ++; //공백기준 몇 번째 문자인지 센다. } return s;} [Level 2][C++] N개의 최소공배수 #include #include #include using namespace std;int solution(vector arr) { //제일 큰 수를 알기 위해 정렬. sort(arr.begin(), arr.end()); int answer = 1; //제일 큰 수 (arr.back())은 아래 반복문의 결과에 따라 바뀐다. for (int i = 2; i 맨위 arr.back()에도 영향을 줌. } } return answer;} [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로 계속 나눈다. 그리고 나눌때마나 나오는 나머지 만큼 이동한다. [Level 2][C++] 영어 끝말잇기 #include #include #include using namespace std;//해쉬 테이블에 문자열을 집어 넣는 함수bool inputTable(string s, vector>& hashT) { //해시 키는 맨 앞에 있는 알파벳이다. int key = s.front()-'a'; if(hashT[key].empty()) //비어있으면 집어넣고 끝. { hashT[key].push_back(s); return true; } else { for(int i=0; i solution(int n, vector words) { //맨 앞 알파벳에 따라 분류되도록 해시 테이블을 만들었다. 26은 알.. [Level 2][C++] 구명보트 #include #include #include #include using namespace std;int solution(vector people, int limit) { //우선 사람들을 무게로 내림차순으로 정렬한다. 큰->작은 sort(people.begin(), people.end(), greater());//전체 사람 수에서 반을 나누고. 그 중 제일 작은 사람의 무게를 저장한다.//왜냐하면 다른 사람에게 더해졌을 경우 0으로 만들 것이기 때문에. int minM = people[people.size()/2];//딱 반만큼. 크기가 큰 사람들끼리 Limit을 넘지 않는 범위에서 합친다. for (int i = 0; i limit) brea.. [Level 1][C++] N으로 표현 #include #include #include using namespace std;int solution(int N, int number) { vector> arr(8); //vector 한 줄당 1개의 N을 씀. N을 쓸 수 있는 건 최대 8번 if (N == number) return 1; else arr[0].push_back(N); //맨 첫번째 줄에 N을 집어넣음. int i = 1; //N 2개 사용 부터 계산 while (i 풀고나서 다른 사람들의 풀이를 보니 재귀함수를 정의해서 푸시는 분들이 많았다.내 방식은 N을 최소 5번 써야 답이 나온다면, N을 1번만 쓴 답과 4번만 쓴 답을 계산 시킨다. 또 2번만 쓰고 .. 이전 1 2 3 4 5 6 ··· 46 다음