Binary Watch
描述
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
分析:数据范围很小,暴力即可;
代码:#include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #define rep(i,m,n) for(i=m;i<=n;i++) #define pii pair<int,int> #define fi first #define se second const int maxn=3e4+10; using namespace std; int n,m; int work(int a,int b) { int cnt=0; while(a) { if(a&1)cnt++; a>>=1; } while(b) { if(b&1)cnt++; b>>=1; } return cnt; } int main() { int i,j,k,t; scanf("%d",&n); rep(i,0,23)rep(j,0,59) { if(work(i,j)==n)printf("%02d:%02d ",i,j); } //system("pause"); return 0; }