LIKE CODING: MJ [17] LED clock
Consider a LED clock. Eg., 06:30 is displayed by an array of LEDs as 110:11110. Note that the clock only needs 10 LEDs since the maximum hour is 12 (1100) and the maximum minute is 60 (111100). Write program to print all times which turns on n LEDs.
Read full article from LIKE CODING: MJ [17] LED clock
Consider a LED clock. Eg., 06:30 is displayed by an array of LEDs as 110:11110. Note that the clock only needs 10 LEDs since the maximum hour is 12 (1100) and the maximum minute is 60 (111100). Write program to print all times which turns on n LEDs.
bool valid(vector<int> led){
return
(toBin(led, 0, 3)<=12 && toBin(led, 4, 9)<=60);
}
int toBin(vector<int> led, int s, int e){
int res = 0;
for
(int i=s; i<=e; ++i){
res <<= 1;
res |= led[i];
}
return
res;
}
void bt(int n, vector<int> led, int count, int pos, vector<vector<int>> &ret){
if
(count==n && valid(led)){
ret.push_back(led);
}
else
if
(pos<10 && count<n){
for
(int i=pos; i<10; ++i){
led[i] = 1;
bt(n, led, count+1, i+1, ret);
led[i] = 0;
}
}
}
vector<vector<int>> getLEDs(int n){
vector<vector<int>> ret;
vector<int> led(10, 0);
bt(n, led, 0, 0, ret);
return
ret;
}