时间限制: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 = ; i < (<<); i++) {
if (__builtin_popcount(i) == x) {
int hour = i >> ;
int minute = i & 0x3f;
if (hour < && minute < ) {
cout << setw() << setfill('') << hour << ":" << setw() << setfill('') << minute << endl;
}
}
}
return ;
}

代码2

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

#include<bits/stdc++.h>
using namespace std;
int cnt1(int x) {//cnt=__builtin_popcount(x);
int cnt = ;
while(x > ) {
if(x % )
cnt++;
x /= ;
}
return cnt;
}
int main() {
int x;
int a[];
scanf("%d",&x);
for(int i=;i<;i++)
a[i]=cnt1(i);
for(int i=;i<;i++){
for(int j=;j<;j++){
if(a[i]+a[j]==x)
printf("%02d:%02d\n",i,j);
}
}
}

Hihocoder1350-Binary Watch的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  5. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  8. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  10. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

随机推荐

  1. Elasticsearch学习(一)————简单命令

    Elasticsearch一.简介**Elasticsearch 是一个分布式的搜索和分析引擎,可以用于全文检索.结构化检索和分析,并能将这三者结合起来.Elasticsearch 基于 Lucene ...

  2. 仅前端cookie之记住密码

    参考文章给忘了...,我就在他基础上修改了一些,但至于安全性,我没弄md5,所以安全系数应该为0 <!DOCTYPE html> <html lang="en"& ...

  3. Brain Network (hard) CodeForces - 690C 简单倍增 + 一些有趣的推导

    Code: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...

  4. javaScript原型、闭包和异步操作

    同学们,这篇博客有点水了,并不是说我不想写这块的内容,是因为查了很多资料,看了很多帖子之后,发现园内王福朋老师写的这系列文章真的很好,他的这系列的博客我已经看了3.4遍了,每一次都有新的收获,我可写不 ...

  5. 【vue】v-if和v-show的区别

    今天来捋一下vue中的v-if与v-show的区别 先来看一下vue官方文档对他们的解释 2.从实现方式来看: v-if是当依赖的值变为false时,直接让元素消失,html代码也会消失,相当于直接在 ...

  6. Linux 密码的暴力破解

    Linux 的密码的介绍 两个文件 1 . /etc/passwd 2 . /etc/shadow ## 关于/etc/shadow 文件的介绍 1 . 第一个字段是用户名 2 . 第二字字段是加密的 ...

  7. 域名系统(DNS)

    DNS (domain name server/system) 1.基本信息 网络中数据通信依赖ip地址 测试:手动将dns服务地址改为空值,通过ip和域名分别测试网络的联通性 FQDN 完全域名(完 ...

  8. phpEXCEL如何设置单元格格式为百分比

    $objExcel->getActiveSheet()->getStyle('C9')->getNumberFormat()->setFormatCode(PHPExcel_S ...

  9. 如何计算合适的InnoDB log file size

    原文链接:http://www.mysqlperformanceblog.com/2008/11/21/how-to-calculate-a-good-innodb-log-file-size/ Pe ...

  10. dtd对xml没有起到约束作用

    问题如题. dtd: xml: BUG很明显,但是xml并没有提示错误信息.xml文档校验设置正常. 此处原因: dtd中元素与子元素设置之间缺少空格: 加上空格后正常报错: