본문 바로가기

코딩테스트 준비105

[프로그래머스][C++] 공배수 #include #include using namespace std; int solution(int number, int n, int m) { int answer = 0; if(number % n == 0 && number % m == 0) return 1; return answer; } 1. %연산을 이용해 나머지가 0인 경우를 판별하여 배수인지를 확인한다. 2. number를 n으로 나눴을 때의 나머지가 0이면서 m으로 나눴을 때도 나머지가 0이라면 1을 리턴한다. 3. 그 외의 경우에는 0을 리턴한다. 잠깨기 용으로 쉽게 풀었다. 2023. 10. 12.
[프로그래머스][C++] 덧칠하기 #include #include using namespace std; int solution(int n, int m, vector section) { int answer = 0; int index = 0; for(int i = section[index++]; i 리턴 if(index >= section.size()) return answer; } i = section[index]; // 현재 섹션을 반복자로 재정의 } return answer; } 1. answer과 index를 선언 2. 반복자 i가 섹션의 0번째 인덱스부터 시작해 n이 될 때까지 반복을 진행한다. 3. 매 반복은 페인트칠을 하는 횟수를 의미하므로 answer을 1씩 증가시킨다. 4. 이후 섹션의 다음 수가 현재 수에 롤러의 길이를 더.. 2023. 9. 13.
[프로그래머스][C++] 카드 뭉치 #include #include using namespace std; string solution(vector cards1, vector cards2, vector goal) { string answer = "Yes"; int x = 0; // card1에서 나와야하는 단어의 인덱스 번호 int y = 0; // card2에서 나와야하는 단어의 인덱스 번호 for(int i = 0 ; i < goal.size(); i++) { string curWord = goal[i]; if(curWord == cards1[x]) { // 현재 단어가 카드뭉치 1에 있는 경우 x++; } else if(curWord == cards2[y]) { // 현재 단어가 카드뭉치 2에 있는 경우 y++; } else { ans.. 2023. 9. 12.
[프로그래머스][C++] 추억 점수 #include #include #include using namespace std; vector solution(vector name, vector yearning, vector photo) { vector answer; map scores; for(int i = 0 ; i < name.size(); i++) { // 이름별 그리움 점수 저장 scores[name[i]] = yearning[i]; } for(int i = 0; i < photo.size(); i++) { answer.push_back({0}); for(int j = 0; j < photo[i].size(); j++) { string curName = photo[i][j]; answer[i] += scores[curName]; } } r.. 2023. 9. 12.
[백준/BOJ][C++] 20920번 영단어 암기는 괴로워 #include #include #include #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N, M; cin >> N >> M; map freq; // 단어별 빈도수(단어, 빈도수) int maxFreq = 0; // 최대 빈도수 for (int i = 0; i > word; // 길이가 M보다 작으면 외우지 않음 if (word.length() < M) continue; freq[word]++; // 단어별 빈도수 체크 maxFr.. 2023. 9. 10.
[백준/BOJ][C++] 2108번 통계학 #include #include #include #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; vector v; map m; // int sum = 0; // 합계 for (int i = 0; i > x; sum += x; // 각 수들의 합을 저장 v.push_back(x); // 처음 나온 수인 경우에는 1로 초기화 나왔던 경우에는 1씩 증가 if (!m[x]) m[x] = 1; else m[x]++.. 2023. 9. 6.
[백준/BOJ][C++] 26069번 붙임성 좋은 총총이 #include #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; map rainbowDancing; // 무지개 댄스를 추고있는 사람 rainbowDancing.insert({ "ChongChong", true }); // 총총이를 추가 int count = 1; for (int i = 0; i > a >> b; if ((!rainbowDancing[a] && !rainbowDancing[b]) || (rainbo.. 2023. 9. 5.
[백준/BOJ][C++] 25192번 인사성 밝은 곰곰이 #include #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; map user; int count = 0; for (int i = 0; i > s; if (s == "ENTER") { user.clear(); } else { if (!user[s]) // 여기서 user[s]가 없는 경우, user[s] = false로 자동 생성됨 { // 처음 채팅치는 유저인 경우 user[s] = true; count++; } }.. 2023. 9. 4.
[백준/BOJ][C++] 1037번 약수 #include #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; int maxNum = 0; int minNum = 1000000; for (int i = 0; i > x; maxNum = max(x, maxNum); minNum = min(x, minNum); } cout 2023. 9. 4.
[백준/BOJ][C++] 1010번 다리 놓기 #include #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T; cin >> T; vector output; for (int i = 0; i > N >> M; int gap = min(M - N, N); long long x = 1; // 분자 long long y = 1; // 분모 for (int j = 1; j 2023. 8. 31.
[백준/BOJ][C++] 11050번 이항 계수 1 #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N, K; cin >> N >> K; int gap = min(N - K, K); int x = 1; // 분자 int y = 1; // 분모 for (int i = 1; i 2023. 8. 31.
[백준/BOJ][C++] 24723번 녹색거탑 - for문 사용 #include using namespace std; int main() { // 쓰레드 환경이 아닐때 버퍼를 분리하여 처리속도를 빠르게 해줌 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; int output = 1; for (int i = 1; i N; cout 2023. 8. 30.