본문 바로가기

전체 글332

평균 구하기 *평균 구하기. 정수를 담고 있는 배열 arr의 평균값을 리턴. #include #include #include using namespace std; double solution(vector arr) { double answer = accumulate(arr.begin(), arr.end(), 0); return answer / arr.size(); } Colored by Color Scriptercs 2019. 9. 5.
제일 작은 수 제거하기 *제일 작은 수 제거하기. 가장 작은 수를 제거. 빈 배열이면 -1을 채워 리턴. #include #include #include using namespace std; vector solution(vector arr) { vector answer; vector::iterator iter; iter = min_element(arr.begin(), arr.end()); arr.erase(iter); if(arr.size() == 0) arr.push_back(-1); answer = arr; return answer; } Colored by Color Scriptercs 2019. 9. 5.
정수 제곱근 판별 *정수 제곱근 판별 임의의 정수 n에 대해 어떤 정수 x의 제곱인지 아닌지 판단. n이 x의 제곱이면 x+1의 제곱 리턴. 아니면 -1 리턴. #include #include using namespace std; long long solution(long long n) { long long answer = 0; double d = sqrt(n); int i = sqrt(n); if(d - i == 0) answer = pow(i + 1, 2); else answer = -1; return answer; } Colored by Color Scriptercs 2019. 9. 5.
체육복 *체육복(탐욕법 Greedy) 전체 학생의 수 n, 체육복을 도난당한 학생 lost[], 여분 학생 reserve[] reserve[] +1, -1 번호 학생에게 빌려줄수 있음. 수업을 들을 수 있는 학생의 최대값 #include #include #include using namespace std; int solution(int n, vector lost, vector reserve) { int answer = 0; vector in; in.assign(n + 2, 1); for(int i = 0; i 2019. 9. 5.
H-index *H-Index(정렬) 논문 n편 중, h번 이상 인용된 논문이 h편 이상 나머지 논문이 h번 이하 인용되었다면 h = H-Index. #include #include #include using namespace std; int solution(vector citations) { int answer = 0; sort(citations.begin(), citations.end(), greater()); int p = 0; while(true) { if(p >= citations[p]) break; p++; } return answer = p; } Colored by Color Scriptercs 2019. 9. 5.
가장 큰 수 *가장 큰 수(정렬 & 문자열) 주어진 배열의 값들로 최대값 만들기. #include #include #include using namespace std; bool compareNum(int a, int b) { string s1 = to_string(a); string s2 = to_string(b); return s1 + s2 > s2 + s1; } string solution(vector numbers) { string answer = ""; sort(numbers.begin(), numbers.end(), compareNum); for(auto elem : numbers) { answer += to_string(elem); } if(answer[0] == '0') answer = '0'; retu.. 2019. 9. 5.