본문 바로가기

Study/BOJ와 Programmers

[BOJ] 18312 시각 c++ (시간문제, string)

 

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