본문 바로가기

알고리즘34

52] 프로그래머스 정수 제곱근 판별 #include using namespace std; long long solution(long long n) { long long answer = 0; if(sqrt(n) - int(sqrt(n)) == 0 ) return pow(sqrt(n)+1, 2) ; else return -1; } 2020. 10. 15.
51] 프로그래머스 콜라츠 추측 #include #include #include using namespace std; int solution(int num2) { int answer = 0; long long num = num2; while(num != 1){ if(num %2 == 0){ num /=2; answer++; } else{ num *= 3; num++; answer++; } if(answer >= 500) break; } if(answer >=500) answer =-1; return answer; } int로 하면 계산 중 int의 범위를 넘어서는 경우가 있기에 long long으로.. 2020. 10. 14.
46★] 프로그래머스 같은 숫자는 싫어 #include #include using namespace std; vector solution(vector arr) { arr.erase(unique(arr.begin(), arr.end()), arr.end()); return arr; } stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector What's the most efficient way to erase duplicates and sort a vector? I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sor.. 2020. 9. 15.
45] 프로그래머스 2016년 코딩테스트 연습 기초부터 차근차근, 직접 코드를 작성해 보세요. programmers.co.kr 각 배열 0번째값 조심 0, THU. #include using namespace std; string solution(int a, int b) { string answer = ""; int arr[13] = {0,31, 29,31,30,31,30,31,31,30,31,30,31}; string date[7] = { "THU", "FRI", "SAT", "SUN", "MON" ,"TUE", "WED"}; int cnt = 0; for(int i = 0;i 2020. 9. 15.