Flip the Bits(思维)
You are given a positive integer n. Your task is to build a number m by flipping the minimum number of bits in the binary representation of n such that m is less than n (m < n) and it is as maximal as possible. Can you?
The first line contains an integer T (1 ≤ T ≤ 105) specifying the number of test cases.
Each test case consists of a single line containing one integer n (1 ≤ n ≤ 109), as described in the statement above.
For each test case, print a single line containing the minimum number of bits you need to flip in the binary representation of n to build the number m.
2
5
10
1
2 题目意思:将一个2进制的n中每个位翻转得到一个比n小且尽可能大的数,求输出翻转了几位。 解题思路:这道题该由我背锅,我当时先是翻译错了题意,后来稍微有一点眉目了,我又理解错了那个flip的意思,这里面的翻转并不是那种交换(swap那样的),而是像硬币正面换到反面那样的翻转,也就
是0与1的交换,根据题意可以推出想要得到一个既要比n小还有尽可能大的数,只有是n前面的那一个数n-1。所以就是根据n构造一个二进制的n-1,方法就是找到n的二进制中最后面的那一个1翻转为0,而最
后一个1之后的0全都翻转成1,统计所用的翻转次数即可。
#include<cstdio>
#include<cstring>
int main()
{
int t,n,j,k,i,count;
int a[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,-,sizeof(a));
j=;
count=;
i=n;
while(i)
{
a[j]=i%;
if(a[j]==)
{
count++;
}
if(a[j]==)
{
count++;
break;
}
i/=;
j++;
}
printf("%d\n",count);
}
return ;
}
Flip the Bits(思维)的更多相关文章
- The Bits (思维+找规律)
Description Rudolf is on his way to the castle. Before getting into the castle, the security staff a ...
- 476. Number Complement 二进制中的相反对应数
[抄题]: Given a positive integer, output its complement number. The complement strategy is to flip the ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- C/C++中几种操作位的方法
参考How do you set, clear and toggle a single bit in C? c/c++中对二进制位的操作包括设置某位为1.清除某位(置为0).开关某位(toggling ...
- The Aggregate Magic Algorithms
http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...
- C++ Bitsets
C++ Bitsets给程序员提供一种位集合的数据结构.Bitsets使用许多二元操作符,比如逻辑和,或等. Constructors 创建新bitsets Operators 比较和赋值bitset ...
- BitSet
前几天干了一件比较无聊的事儿——抄了一遍C++ STL bitset的源代码,把不懂的宏定义去掉了,发现(暂时)还能用,嘿嘿. #ifndef BITSET_H #define BITSET_H #i ...
- Hamming code
Also known as (7,4) code,7 trainsmitted bits for 4 source code. TRANSMIT The transmitted procedure c ...
- C++ 标准模板库(STL)
C++ 标准模板库(STL)C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), ...
随机推荐
- java8 新特性 Stream流 分组 排序 过滤 多条件去重
private static List<User> list = new ArrayList<User>(); public static void main(String[] ...
- flask中请求勾子
请求勾子 在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要处理,比如: *在请求开始时,建立数据库连接; *在请求开始时,根据需求进行权限校验; *在请求结束时,指定数据的交互格式; 为了让每 ...
- laravel5.5源码笔记(一、入口应用的初始化)
laravel的项目入口文件index.php如下 define('LARAVEL_START', microtime(true)); require __DIR__.'/../vendor/auto ...
- Hive(10)-文件存储格式
Hive支持的存储数据的格式主要有:TEXTFILE .SEQUENCEFILE.ORC.PARQUET 一. 列式存储和行式存储 左边为逻辑表,右边第一个为行式存储,第二个为列式存储 1. 行式存储 ...
- 坚果云WebDav示例
坚果云WebDav示例 最近看到坚果云有一个WebDAV应用,一时不解这是什么功能,了解后做了一个示例: WebDAV是一种基于HTTP1.1协议的通信协议.它扩展了HTTP1.1,在GET.POST ...
- angular中的$cookies和$cookieStore设置过期时间
angular1.4及以上版本才支持$cookies. 项目引入的是1.4.2版本,操作cookies原先一直用的是$cookieStore,用的飞起啊. $cookieStore.remove(&q ...
- HTTP1.0,HTTP1.1,HTTP2.0的主要特征对比
HTTP1.0 是一种无状态.无连接的应用层协议. HTTP1.0规定浏览器和服务器保持短暂的连接,浏览器的每次请求都需要与服务器建立一个TCP连接,服务器处理完成后立即断开TCP连接(无连接),服务 ...
- python2.7入门---XML解析
首先我们先来考虑,什么是XML?XML 指可扩展标记语言(eXtensible Markup Language).XML 被设计用来传输和存储数据.XML是一套定义语义标记的规则,这些标记将文 ...
- 20155235 2016-2017-1 《Java程序设计》第3周学习总结
20155235 2016-2017-1 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 类与对象 定义类 使用标准类 对象指定与相等性 基本类型打包器 打包基本类 ...
- 20155328 2016-2017-2 《Java程序设计》第四周学习总结
学号 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 继承:避免多各类间重复定义行为,extends关键字表示继承后再扩充原本没有的行为.如果没有使用exte ...