zoukankan      html  css  js  c++  java
  • Hihocoder1350-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

    题意

    输出化成二进制(时化为5位,分化为6位)后有合起来x个1的所有时间

    思路

    这里需要用到一个glibc内置函数__builtin_popcount(n),表示十进制的n化成2进制后的1的个数,顺便普及一下其他几个内置函数
    http://www.cnblogs.com/nysanier/archive/2011/04/19/2020778.html
    然后遍历一遍,具体实现看代码

    代码1

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

     代码2

    正常思想,不需要什么函数,位运算都不用。。

    #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
    ",i,j);
            }
        }
    }
  • 相关阅读:
    数据库——Oracle(7)
    数据库——Oracle(6)
    数据库——Oracle(5)
    数据库——Oracle(4)
    数据库——Oracle(3)
    数据库——Oracle(2)
    Java 学习笔记之 Synchronized锁对象
    Java 学习笔记之 线程安全
    Java 学习笔记之 实例变量非线程安全
    Java 学习笔记之 方法内的临时变量是线程安全
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6279485.html
Copyright © 2011-2022 走看看