본문 바로가기
  • Let's study

PS/백준56

[C++] 백준 2941번: 크로아티아 알파벳 1. 크로아티아 알파벳을 배열로 저장해준다. 2. string으로 문자열을 입력받고 첫 글자가 크로아티아의 알파벳과 동일하다면 다음 글자도 비교한다. 이 때 크로아티아 알파벳이 맞다면 i++를 해주며 넘긴다. (세글자인 "dz="는 i+=2를 해준다.) 3. for문을 한 번 돌 때마다 알파벳 수를 세는 cnt를 +1 해준다. (한 글자짜리 알파벳이어도 cnt++가 됨.) #include #include using namespace std; int main() { string str[] = {"c=","c-","dz=","d-","lj","nj","s=","z="}; string arr; cin >> arr; int cnt = 0; for (int i = 0; i < arr.size(); i++) { i.. 2020. 12. 8.
[C++] 백준 2667번: 단지번호붙이기 dfs 이용 1) 먼저 map 돌면서 1이면서 방문하지 않은 공간 찾기 2) dfs로 집 개수 계산 3) 벡터 house에 집 개수 저장하고 house.size()로 단지 개수 구하기 #include #include #include using namespace std; int map[26][26]; bool visit[26][26]; int n, cnt; int dx[4] = { 1,-1,0,0 };//동서남북 int dy[4] = { 0,0,-1,1 }; vectorhouse; int dfs(int i, int j) { visit[i][j] = true; cnt++; for (int k = 0; k < 4; k++) { int nx = i + dx[k]; int ny = j + dy[k]; if (nx.. 2020. 11. 29.
[C++] 백준 2178번: 미로 탐색 최단 거리를 찾아야 하므로 BFS 사용 visit 배열로 이동 횟수 계산 및 방문 확인 #include #include #include #include using namespace std; int map[101][101]; queueq; int visit[101][101] = { 0, }; int n, m; int dx[4] = { 1,-1,0,0 };//동서남북 int dy[4] = { 0,0,-1,1 }; int bfs(int i, int j) { q.push(make_pair(i, j)); visit[i][j] = 1; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); if (x == n && y == m.. 2020. 11. 29.
[C++] 백준 1697번: 숨바꼭질 #include #include using namespace std; queue q; int visited[100001] = { 0, }; int n, k; int bfs() { q.push(n); visited[n] = 1; while (!q.empty()) { int f = q.front();//n q.pop(); if (f == k) { return visited[f]-1; } if (f + 1 = 0 && visited[f - 1]==0) { q.push(f - 1); visited[f - 1] = visited[f] + 1; } if (.. 2020. 11. 29.
[C++] 백준 16496번: 큰 수 만들기 1) 입력받을 때 0의 개수를 세고 리스트에 포함된 수와 개수가 같다면 0 출력 2) 0만 입력받은 게 아니라면 sort를 사용하여 앞 숫자가 a, 뒷 숫자가 b라 할 때 ab와 ba를 비교하여 정렬 #include #include #include using namespace std; string arr[1001]; bool cmp(string a, string b) { if (a == b) return false; string ab = a + b; string ba = b + a; if (ab > ba) return true; else return false; } int main() { ios_base::sync_with_stdio; int n; int zero = 0; cin >> n; for (in.. 2020. 11. 23.
[C++] 백준 1676번: 팩토리얼 0의 개수 0이 나오려면 2x5이여야 한다. 2가 5보다 많이 나오므로 5만 센다. 따라서 5, 25, 125으로 나눈 몫을 센다. #include #include using namespace std; int arr[1000001] = { 0, }; int main() { ios_base::sync_with_stdio; cin.tie(NULL); int n; cin >> n; int result = n / 5 + n / 25 + n / 125; cout 2020. 11. 22.