描述
Consider a binary watch with 5 binary digits to display hours (00 - 23) and 6 binary digits to display minutes (00 - 59).
For example 11:26 is displayed as 01011:011010.
Given a number x, output all times in human-readable format "hh:mm" when exactly x digits are 1.
输入
An integer x. (0 ≤ x ≤ 9)
输出
All times in increasing order.
1样例输出
00:01 00:02 00:04 00:08 00:16 00:32 01:00 02:00 04:00 08:00 16:00
dfs枚举所有情况。
感觉我越来越水了。
代码:
#include <bits/stdc++.h>
using namespace std;
int a[5]={1,2,4,8,16};
int b[6]={1,2,4,8,16,32};
int v1[10];
int v2[10];
int dp[25][66];
struct poin
{
int shi,fen;
}ma[110000];
int ans=0;
int cmp(poin q,poin w)
{
if(q.shi<w.shi)return 1;
else if(q.shi==w.shi&&q.fen<w.fen)return 1;
return 0;
}
void dfs(int x,int shi,int fen)
{
if(x<=0)
{
if(dp[shi][fen])return;
if(shi<=23&&fen<=59)
{
dp[shi][fen]=1;
ma[ans].shi=shi;
ma[ans].fen=fen;
ans++;
}
return;
}
if(shi>=24||fen>=60)return;
for(int i=0;i<6;i++)
{
if(!v2[i])
{
v2[i]=1;
dfs(x-1,shi,fen+b[i]);
v2[i]=0;
}
}
for(int i=0;i<5;i++)
{
if(!v1[i])
{
v1[i]=1;
dfs(x-1,shi+a[i],fen);
v1[i]=0;
}
}
}
int main()
{
int x;
cin>>x;
dfs(x,0,0);
sort(ma,ma+ans,cmp);
/*
int vis[11000];
for(int i=1;i<ans;i++)
{
if(ma[i].shi==ma[i-1].shi&&ma[i].fen==ma[i].fen)
{
vis[i]=1;
}
}
*/
for(int i=0;i<ans;i++)
{
printf("%02d:%02d\n",ma[i].shi,ma[i].fen);
}
return 0;
}