https://www.acmicpc.net/problem/18312
✔ 문제 이해
N과 R이 주어진다.
00시 00분 00초부터 N시 59분 59초 까지의 시간 중에서 R이 등장하는 모든 시간들의 수를 구하여라.
✔ 풀이 + 코드
00~N까지의 hour중에서,
00~59까지의 minute중에서,
00~59까지의 second중 K가 포함된 것을 찾기 위해서 for문을 세개 이용한다.
#include <iostream>
using namespace std;
int main()
{
int n, k, cnt = 0;
cin >> n >> k;
string str = "";
for(int h = 0; h <= n; h++){
for(int m = 0; m < 60; m++){
for(int s = 0; s < 60; s++){
// 한 글자인 경우 앞에 0붙이기
if(h / 10 == 0) str += '0';
str += to_string(h);
if(m / 10 == 0) str += '0';
str += to_string(m);
if(s / 10 == 0) str += '0';
str += to_string(s);
if(str.find(to_string(k)) != string::npos) cnt++;
str = "";
}
}
}
cout << cnt << endl;
return 0;
}
✔ 피드백
- 시간은 정수가 아니라 문자열로 풀어야 하는 것같다.
✔ reference
https://what-am-i.tistory.com/96
'Study > BOJ와 Programmers' 카테고리의 다른 글
[BOJ] 2579 계단 오르기 c++ (DP) (0) | 2023.05.01 |
---|---|
[BOJ] 1463 1로 만들기 c++ (dp) (0) | 2023.04.20 |
[BOJ] 11656 접미사 배열 c++ (priority_queue) (0) | 2023.04.13 |
[BOJ] 하얀칸 c++ (for문 index 규칙) (0) | 2023.04.07 |
[BOJ] 1068 트리 c++ (트리 그래프, int형 dfs문 활용) (0) | 2023.04.06 |