본문 바로가기

분류 전체보기

(274)
[Level 1, C++] 체육복 진짜 까다로웠던 문제였다. 내 방식대로 도저히 답이 안나와서 같은 과 형에게 알고리즘을 물어보고 나니까 풀 수 있었던 문제다. #include #include #include using namespace std; int solution(int n, vector lost, vector reserve) { int answer = 0; vector student(n, 1); // studnet 배열에 각 학생의 번호와 체육복 갯수를 초기화 for (int i = 0; i < lost.size(); i++) { student[lost[i] - 1]--; } for (int i = 0; i < reserve.size(); i++) { student[reserve[i] - 1]++; } for (int i = 0;..
[Level 1, C++] 최대공약수와 최소공배수 문제 : 최대공약수와 최소공배수 풀이 방법 유클리드 호제법을 쓰지 않으면 시간 오류가 뜨기 때문에 유클리드 호제법을 찾아봐야한다. 공식을 이해하느라 유튜브도 찾아보고 그랬었긴 했다. 원리를 설명하는 영상은 이 영상이 최고인듯하다. 풀이 코드 #include #include using namespace std; vector solution(int n, int m) { vector answer(2, 1); int a, b, tmp; if(n > m) { tmp = m; m = n; n = tmp; } a = n, b = m, tmp = 0; while(b % a != 0) { tmp = b % a; b = a; a = tmp; } answer[0] = a; answer[1] = (n * m) / answer..
[Level 1, C++] 시저 암호 시저 암호는 유명한데 교양 보안 관련 수업에서도 나왔었다. 재밌는 암호화 방법인 것 같다. #include #include #include using namespace std; string solution(string s, int n) { string answer = ""; for (int i = 0; i < s.size(); i++) { if (s[i] == ' ') { answer.push_back(s[i]); } else { for (int u = 0; u < n; u++) { if (s[i] == 'z') { s[i] = 'a'; continue; } else if (s[i] == 'Z') { s[i] = 'A'; continue; } else { s[i] += 1; continue; } } an..
[Level 1, C++] 자연수 뒤집어 배열로 만들기 #include #include using namespace std; vector solution(long long n) { vector answer; string str = to_string(n); for(int i = str.length() - 1; i >= 0; i--) { answer.push_back(str.at(i) - 48); } return answer; }
[Level 1, C++] x만큼 간격이 있는 n개의 숫자 #include #include #include using namespace std; vector solution(int x, int n) { vector answer; long long temp = x; if(x == 0) { for(long long i=0; i
[Level 1, C++] 핸드폰 번호 가리기 #include using namespace std; string solution(string phone_number) { string answer = phone_number; for(int i=0; i
[Level 1, C, C++] 하샤드 수 C랑 C++ 같이 풀어보았다. #include #include #include bool solution(int x) { bool answer = true; int x_sum = 0; char x_char[10000]; sprintf(x_char, "%d", x); for (int i = 0; i < strlen(x_char); i++) { x_sum += (int)x_char[i] - 48; } if (x % x_sum == 0) answer = true; else answer = false; return answer; } #include using namespace std; bool solution(int x) { int sum = 0; string s_number = to_string(x); for(..
[Level 1, C++] 정수 내림차순으로 배치하기 #include #include #include using namespace std; long long solution(long long n) { long long answer = 0; string str = to_string(n); sort(str.begin(), str.end(), greater( )); answer = stoull(str); return answer; }