Binary Watch_太阁竞赛A

版权声明:本文为博主原创文章,未经博主允许不得转载,谢谢合作! https://blog.csdn.net/cleopard66/article/details/52081792
题目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  

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;
}


没有更多推荐了,返回首页