时间限制: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. UWP添加数字证书导出安装包本地安装

    先生成一个简单的HelloWorld应用程序 <Page x:Class="HelloWorld.MainPage" xmlns="http://schemas.m ...

  2. logging模块、shutil模块、subprocess模块、xml模块

    logging模块 shutil模块 subprocess模块 xml模块 logging模块 函数式简单配置 import logging logging.debug('debug message' ...

  3. 【转】Oracle基础结构认知—oracle物理结构 礼记八目 2017-12-13 20:31:06

    原文地址:https://www.toutiao.com/i6499008214980362765/ oracle数据库启动:oracle服务启动,通过参数文件查找控制文件,启动控制文件,则控制文件调 ...

  4. 洛谷P1425 小鱼的游泳时间

    题目描述 伦敦奥运会要到了,小鱼在拼命练习游泳准备参加游泳比赛,可怜的小鱼并不知道鱼类是不能参加人类的奥运会的.这一天,小鱼给自己的游泳时间做了精确的计时(本题中的计时都按24小时制计算),它发现自己 ...

  5. Mysql 索引-1

    索引的类型 根据数据库的功能,可以在数据库设计器中创建四种索引:唯一索引.非唯一索引.主键索引和聚集索引. 索引的不同应用场景 场景 1. 当数据多且字段值有相同的值得时候用普通索引. 2. 当字段多 ...

  6. C#中的Socket

    ];//用于缓存客户端所发送的信息,通过socket传递的信息必须为字节数组 IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);//本机预使用 ...

  7. lim的日常生活

     

  8. ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes

    http://www.codeproject.com/Articles/775220/ASP-Net-MVC-What-are-the-uses-of-Display-DisplayNa?utm_so ...

  9. 【解决】run-as: Package &#39;&#39; is unknown

    问题: [2014-07-30 20:20:25 - nativeSensorStl] gdbserver output: [2014-07-30 20:20:25 - nativeSensorStl ...

  10. servletConfig和ServletContext 以及servletContextListener介绍

    <servlet>     <servlet-name>BeerParamTests</servlet-name>     <servlet-class> ...