Skip to content

Commit 6aafcb5

Browse files
read binary watch
1 parent 5838eaa commit 6aafcb5

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/401.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <bitset>
2+
#include <string>
3+
#include <sstream>
4+
#include <vector>
5+
#include <fstream>
6+
7+
class Solution {
8+
//private:
9+
// std::vector<std::string> result;
10+
//
11+
// void helper(int i, int k, int num, std::bitset<4> HOUR, std::bitset<6> MINUTE) {
12+
// if (i > num) {
13+
// auto hour = HOUR.to_ulong();
14+
// auto minute = MINUTE.to_ulong();
15+
// std::ostringstream time;
16+
// time << hour << ":";
17+
// if (minute < 10) time << "0";
18+
// time << minute;
19+
// result.push_back(time.str());
20+
// return;
21+
// }
22+
// if (k < 4) {
23+
// helper(i, k + 1
24+
// for (int j = 0; j < 4; ++j) {
25+
// if (!HOUR[j]) {
26+
// helper(i + 1, num, HOUR.set(j), MINUTE);
27+
// HOUR.reset(j);
28+
// }
29+
// }
30+
// for (int j = 0; j < 6; ++j) {
31+
// if (!MINUTE.test(j)) {
32+
// helper(i + 1, num, HOUR, MINUTE.set(j));
33+
// MINUTE.reset(j);
34+
// }
35+
// }
36+
// }
37+
private:
38+
unsigned int countSetBits(unsigned int n) {
39+
unsigned int count = 0;
40+
while (n)
41+
{
42+
count += n & 1;
43+
n >>= 1;
44+
}
45+
return count;
46+
}
47+
48+
public:
49+
std::vector<std::string> readBinaryWatch(int num) {
50+
std::vector<std::string> result;
51+
for (unsigned int i = 0; i < 12; ++i) {
52+
for (unsigned int j = 0; j < 60; ++j) {
53+
if (countSetBits(i) + countSetBits(j) == num) {
54+
std::ostringstream time;
55+
time << i << ":";
56+
if (j < 10) time << 0;
57+
time << j;
58+
result.push_back(time.str());
59+
}
60+
}
61+
}
62+
return result;
63+
}
64+
};
65+
66+
int main() {
67+
std::ofstream fout("/home/wyx/result.txt");
68+
Solution s;
69+
auto result = s.readBinaryWatch(2);
70+
for (const auto &time : result) {
71+
fout << time << "\n";
72+
}
73+
fout.close();
74+
return 0;
75+
}
76+

src/hellp.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <bitset>
2+
#include <iostream>
3+
using namespace std;
4+
int main() {
5+
bitset<2> b;
6+
b.set(0);
7+
for (int i = 0; i < 2; ++i)
8+
cout << b[i] << endl;
9+
return 0;
10+
}

0 commit comments

Comments
 (0)