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 = ; 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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- 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 ...
- [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 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [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, ...
随机推荐
- 团体程序设计天梯赛-练习集-L1-026. I Love GPLT
L1-026. I Love GPLT 这道超级简单的题目没有任何输入. 你只需要把这句很重要的话 —— “I Love GPLT”——竖着输出就可以了. 所谓“竖着输出”,是指每个字符占一行(包括空 ...
- Android 将Android项目打包成aar文件
参考资料:https://blog.csdn.net/csdn_mm/article/details/78364444
- python 操作jenkins 常用api
Python-Jenkins 继续寻找,然后我在Jenkins官网上找到了Python-Jenkins API,仔细阅读后发现,它几乎涵盖了大部分Jenkins的操作,大大方便了我们在后台进行对Jen ...
- 以checked选中作为判断条件的各种写法
<input type="radio" name="choice" id="ipt1"> <label for=" ...
- 4.2、Ansible常用模块
1.command:命令模块,默认模块,用于在远程执行命令,不支持变量.ansible 192.168.139.128 -a 'date' 2.cron:计划任务模块:ansible 192.168. ...
- YII实现dropDownList 联动事件
因功能需求,需要用到联动,特此记录分享 一.视图中 <div class="main-form"> <?php $form = ActiveForm::begin ...
- C#实现简单的串口通信
前言 本着学习研究的态度,用c#语言实现简单的串口通信工具. 一.串口通信原理 串口通信 串口通信(Serial Communications)的概念非常简单,串口按位(bit)发送和接收字节.尽管比 ...
- Docker学习总结(14)——从代码到上线, 云端Docker化持续交付实践
2016云栖大会·北京峰会于8月9号在国家会议中心拉开帷幕,在云栖社区开发者技术专场中,来自阿里云技术专家罗晶(瑶靖)为在场的听众带来<从代码到上线,云端Docker化持续交付实践>精彩分 ...
- log4j输出多个自定义日志文件,动态配置路径
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- 不可靠信号SIGCHLD丢失的问题
如果采用 void sig_chld(int signo) { pid_t pid; int stat; while((pid = waitp ...