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, ...
随机推荐
- 5G开启百亿“机器人”时代 安全与标准仍待统一
5G开启百亿“机器人”时代 安全与标准仍待统一 5G将会给物联网产业带来滚雪球效应,这将是数以百亿计“机器人”之间的网络. 在5G的助推下,物联网应用正在加速落地. 6月12日,CES Asia 20 ...
- WebStorm 配置 svn
1.下载 SlikSVN. 2.安装.路径 D:\Program Files\slik\bin. 3.在WebStorm中配置 file->settings->Version Co ...
- XSS Chanllenges 6-10
Stage #6 测试代码</xss> 存在过滤,并且也没有其他输入点,尝试构建" onmousemove="alert(document.domain),并查看源代码 ...
- STM32 ADC多通道转换DMA模式与非DMA模式两种方法(HAL库)
一.非DMA模式(转) 说明:这个是自己刚做的时候百度出来的,不是我自己做出来的,因为感觉有用就保存下来做学习用,原文链接:https://blog.csdn.net/qq_24815615/arti ...
- 1、认识和安装MongoDB
MongoDB简介:MongoDB是一个基于分布式文件存储的数据库,由C++语言编写.目的是为WEB应用提供扩展的高性能的数据存储解决方案.MongoDB是一个介于关系型数据库和非关系型数据库之间的产 ...
- ZOJ 2314 Reactor Cooling
Reactor Cooling Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- SQLSERVER数据库还原的时候,报 WITH MOVE 子句可用于重新定位一个或多个文件 的错误,求解决
http://www.flybi.net/question/4070 梁勇 - 天善智能微软BI首席讲师 数据库备份文件还原产生这个错误的原因是:还原目录下存在多个同名文件, 如图所示,只需要将第2个 ...
- Hadoop(2)_机器信息分布表
1.分布式环境搭建 采用4台安装Linux环境的机器来构建一个小规模的分布式集群. 图1 集群的架构 其中有一台机器是Master节点,即名称节点,另外三台是Slaver节点,即数据节点.这四台机器彼 ...
- 定时任务为什么不用Timer
在做定时任务的时候,有的同学可能能会用到Timer这个定时任务的辅助类, 可是使用它会有潜在的风险,风险例如以下, (1)时间计算不准确问题 由于Timer是以绝对时间计算定时任务的,会受到系 ...
- Android 经常使用设计模式(一)
由于项目变更的频繁性,作为一名程序猿,我们须要掌握设计模式的必要性.就不言而喻~~.以下就是一些我自己学习的设计模式总结. 接下来,主要是针对几个比較经常使用模式进行解说,主要是以下几种: 观察者模式 ...