時間限制:10000ms
單點時限:1000ms
內存限制:256MBhtml
Consider a binary watch with 5 binary digits to display hours (00 - 23) and 6 binary digits to display minutes (00 - 59).c++
For example 11:26 is displayed as 01011:011010. git
Given a number x, output all times in human-readable format 「hh:mm」 when exactly x digits are 1. markdown
An integer x. (0 ≤ x ≤ 9) app
All times in increasing order. ide
1函數
00:01 00:02 00:04 00:08 00:16 00:32 01:00 02:00 04:00 08:00 16:00ui
輸出化成二進制(時化爲5位,分化爲6位)後有合起來x個1的全部時間lua
這裏須要用到一個glibc內置函數__builtin_popcount(n),表示十進制的n化成2進制後的1的個數,順便普及一下其餘幾個內置函數
http://www.cnblogs.com/nysanier/archive/2011/04/19/2020778.html
而後遍歷一遍,具體實現看代碼url
#include <bits/stdc++.h> using namespace std; int main() { int x; cin >> x; for (int i = 0; i < (1<<11); i++) { if (__builtin_popcount(i) == x) { int hour = i >> 6; int minute = i & 0x3f; if (hour < 24 && minute < 60) { cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << endl; } } } return 0; }
正常思想,不須要什麼函數,位運算都不用。。
#include<bits/stdc++.h> using namespace std; int cnt1(int x) {//cnt=__builtin_popcount(x); int cnt = 0; while(x > 0) { if(x % 2) cnt++; x /= 2; } return cnt; } int main() { int x; int a[60]; scanf("%d",&x); for(int i=0;i<60;i++) a[i]=cnt1(i); for(int i=0;i<24;i++){ for(int j=0;j<60;j++){ if(a[i]+a[j]==x) printf("%02d:%02d\n",i,j); } } }