时间限制: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. System.Windows.Forms.WebBrowser中 处理 js 脚本 window.Open 禁止新建窗口 的方法

    wb 是 拖放在窗体上的 System.Windows.Forms.WebBrowser 在你的窗体代码中定义 SHDocVw.WebBrowser_V1 wb1; 在 你窗体的 load 事件中 加 ...

  2. gitlab安装和汉化

    Centos 7.x 安装 gitlab-ce-8.8.0-ce 一.安装配置依赖项 yum -y install curl unzip policycoreutils git wget # 依赖包 ...

  3. jmeter图片的下载

    1.jmeter下载文件 首先添加一个线程组,然后在线程组里面添加一个http请求,因为是获取数据,所有是get请求,写好下载的地址 1.添加线程组 :右键测试计划,添加-Threads(Users) ...

  4. JavaScript进阶【三】JavaScript面向对象的基础知识复习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. [网络流24题] 最长k可重线段集问题 (费用流)

    洛谷传送门 LOJ传送门 最长k可重区间集问题的加强版 大体思路都一样的,不再赘述,但有一些细节需要注意 首先,坐标有负数,而且需要开$longlong$算距离 但下面才是重点: 我们把问题放到了二维 ...

  6. [Libre 6281] 数列分块入门 5 (分块)

    水一道入门分块qwq 题面:传送门 开方基本暴力.. 如果某一个区间全部都开成1或0就打上标记全部跳过就行了 因为一个数开上个四五六次就是1了所以复杂度能过233~ code: //By Menteu ...

  7. Linux学习一:图解CentOS 6.5安装步骤

    1 进入安装界面 2 选择语言 3 选择键盘 4 选择存储类型 5 是否格式化硬盘 6 设置主机名 7 配置网卡 (1)选择网卡并编辑 (2)配置IPv4 (3)查看虚拟网络编辑器 NAT设置 DHC ...

  8. springboot 不使用前端模板直接跳转页面

    1.创建springboot项目 2.在resource 下创建pages文件夹,存放所有页面 3.编写后台代码 4.访问http://localhost:8080/index,即可跳转到页面

  9. 在Eclipse中创建Maven多模块项目

    在Eclipse中创建Maven多模块项目1,创建多模块项目选择File>New>Project,打开New Project窗口,选择Maven>Maven Project,选择下一 ...

  10. 有关文档碎片(document fragment)的使用方法

    通常情况下改动.删除或者添加DOM元素. 更新DOM会导致浏览器又一次绘制屏幕,也会导 致reflow,这样会带来巨大的开销.我们通常解决这的办法尽量降低更新DOM.这也就意 味着将DOM的改变分批处 ...