반응형
2438번 문제 : 별 찍기
https://www.acmicpc.net/problem/2438
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; int main() { int inp; cin >> inp; for (int i = 0; i < inp; i++) { for(int j = 0; j < i + 1; j++) { cout << "*"; } cout << endl; } return 0; } | cs |
메모:
-endl 대신 cout "\n" 사용하면 4ms 빨라짐
2292번 문제 : 벌집
https://www.acmicpc.net/problem/2292
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; int main() { int inp; cin >> inp; int counter = 0; int num = 1; if (inp != 1) { while (inp > num) { num = num + 6 * counter; counter++; } } else { counter++; } cout << counter; return 0; } | cs |
메모:
-반례 inp : 1때문에 inp !=1 조건문이 필요
1193번 문제 : 분수찾기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> using namespace std; int main() { int inp; cin >> inp; int total = 1; int row = 1; while (total < inp) { row++; total += row; } int col = inp - (total - row); if (row % 2 != 0)//odd { cout << row - col + 1 << "/" << col; } else //even { cout << col << "/" << row - col + 1; } return 0; } | cs |
10250번 문제 : ACM 호텔
https://www.acmicpc.net/problem/10250
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include <iostream> #include <vector> #include <string> using namespace std; int main() { int inp; cin >> inp; vector<string> result; while (inp > 0) { int h, w, n; cin >> h >> w >> n; string roomNum = ""; int counter = 1; int leftovers = n; while (leftovers > h) { counter++; leftovers -= h; } if (counter < 10) { roomNum += to_string(leftovers); roomNum += "0"; roomNum += to_string(counter); } else { roomNum += to_string(leftovers); roomNum += to_string(counter); } result.push_back(roomNum); inp--; } for (auto i : result) cout << i << endl; return 0; } | cs |
1924번 문제 : 2007년
https://www.acmicpc.net/problem/1924
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #include <string> using namespace std; int main(void) { string dayName[7] = { "SUN" , "MON", "TUE", "WED", "THU", "FRI", "SAT" }; int month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int totalDays = 0; int inputM, inputD; cin >> inputM >> inputD; for (int i = 0; i < inputM; i++) totalDays += month[i]; totalDays += inputD; int leftover = totalDays % 7; cout << dayName[leftover]; return 0; } | cs |
2775번 문제 : 부녀회장이 될테야
https://www.acmicpc.net/problem/2775
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> using namespace std; int arr[15][14] = { 0 }; int GetNum(int k, int n) { if (k == 0) return arr[k][n]; else { if (arr[k][n] != 0) return arr[k][n]; else { for (int i = 0; i <= n; i++) { arr[k][n] += GetNum(k - 1 , i); } return arr[k][n]; } } } int main() { for (int i = 0; i < 14; i++) { arr[0][i] = i + 1; } int inp; cin >> inp; while (inp > 0) { int a, b; cin >> a >> b; cout << GetNum(a, b - 1) << endl; inp--; } return 0; } | cs |
메모:
-성능을 위해 메모라이제이션 사용
1475번 문제 : 방 번호
https://www.acmicpc.net/problem/1475
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <iostream> using namespace std; int main() { int set[10] = { 0 }; int inp; cin >> inp; if (inp == 0) { cout << "1" << endl; return 0; } else { while (inp > 0) { int temp = inp % 10; set[temp] ++; inp /= 10; } int maxSet = 0; for (int i = 0; i < 10; i++) { if (i == 6 || i == 9) continue; if (set[i] > maxSet) maxSet = set[i]; } int sixNineSet; if ((set[6] + set[9]) % 2 == 0) sixNineSet = (set[6] + set[9]) / 2; else sixNineSet = (set[6] + set[9]) / 2 + 1; if (sixNineSet > maxSet) maxSet = sixNineSet; cout << maxSet << endl; } return 0; } | cs |
1475번 문제 : 카잉 달력
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <iostream> #include <vector> using namespace std; int GCD(int a, int b) { if (a % b == 0) return b; return GCD(b, a % b); } int LCM(int a, int b) { return (a * b) / GCD(a, b); } int main() { int inp; cin >> inp; vector<int> result; while (inp > 0) { int M, N, x, y; cin >> M >> N >> x >> y; int counter = 1; int maxYear = LCM(M, N); while (true) { if (x > maxYear || (x - 1) % N + 1 == y) break; x += M; } if (x > maxYear) result.push_back(-1); else result.push_back(x); inp--; } for (auto i : result) cout << i << "\n"; return 0; } | cs |
반응형
'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글
소수 구하기 (0) | 2019.02.13 |
---|---|
정렬해보기 (0) | 2019.02.10 |
문자열 사용하기 3 (0) | 2019.02.05 |
문자열 사용하기 2 (0) | 2019.02.03 |
문자열 사용하기 (0) | 2018.11.27 |