题目1 : Binary Watch
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
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
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
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 <iostream>
using namespace std;
int compt(int x)
{
int ans=0;
while (x>0){ if (x%2==1) ans++; x/=2; }
return ans;
}
int main()
{
int n,a[60];
cin>>n;
for (int i=0;i<=59;i++) a[i]=compt(i);
for (int i=0;i<=23;i++)
for (int j=0;j<=59;j++)
if (a[i]+a[j]==n) cout<<i/10 <<i%10 <<':' <<j/10 <<j%10 <<endl;
return 0;
}
本文探讨了一道经典的编程题——二进制手表问题。题目要求根据给定的点亮二进制位数,输出所有可能的时间显示。文章通过具体的代码实现,展示了如何计算并输出这些时间。
1148

被折叠的 条评论
为什么被折叠?



