-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_2437_countTime.cc
59 lines (52 loc) · 1005 Bytes
/
Problem_2437_countTime.cc
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
54
55
56
57
58
59
#include <iostream>
#include <string>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 检查时间是否合法
bool check(string &time)
{
int hh = std::stoi(time);
int mm = std::stoi(time.substr(3));
return hh < 24 && mm < 60;
}
int process(string &time, int index)
{
if (index == time.length())
{
return check(time);
}
int ans = 0;
if (time[index] == '?')
{
// 尝试每一种情况
for (int i = 0; i <= 9; i++)
{
time[index] = '0' + i;
ans += process(time, index + 1);
time[index] = '?';
}
}
else
{
ans = process(time, index + 1);
}
return ans;
}
int countTime(string time) { return process(time, 0); }
};
void testCountTime()
{
Solution s;
EXPECT_EQ_INT(2, s.countTime("?5:00"));
EXPECT_EQ_INT(100, s.countTime("0?:0?"));
EXPECT_EQ_INT(1440, s.countTime("??:??"));
EXPECT_SUMMARY;
}
int main()
{
testCountTime();
return 0;
}