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?

Input

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.

Output

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.

Example
Input
2
5
10
Output
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(思维)的更多相关文章

  1. The Bits (思维+找规律)

    Description Rudolf is on his way to the castle. Before getting into the castle, the security staff a ...

  2. 476. Number Complement 二进制中的相反对应数

    [抄题]: Given a positive integer, output its complement number. The complement strategy is to flip the ...

  3. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  4. C/C++中几种操作位的方法

    参考How do you set, clear and toggle a single bit in C? c/c++中对二进制位的操作包括设置某位为1.清除某位(置为0).开关某位(toggling ...

  5. The Aggregate Magic Algorithms

    http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...

  6. C++ Bitsets

    C++ Bitsets给程序员提供一种位集合的数据结构.Bitsets使用许多二元操作符,比如逻辑和,或等. Constructors 创建新bitsets Operators 比较和赋值bitset ...

  7. BitSet

    前几天干了一件比较无聊的事儿——抄了一遍C++ STL bitset的源代码,把不懂的宏定义去掉了,发现(暂时)还能用,嘿嘿. #ifndef BITSET_H #define BITSET_H #i ...

  8. Hamming code

    Also known as (7,4) code,7 trainsmitted bits for 4 source code. TRANSMIT The transmitted procedure c ...

  9. C++ 标准模板库(STL)

    C++ 标准模板库(STL)C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), ...

随机推荐

  1. 使用vue封装一个tab栏切换的左侧导航栏的公共组件

     首先看最终效果图: 1.compent文件夹里添加tab文件夹,里面创建index.vue index.js index.css index.vue内的template部份代码如下:(最新更正:代码 ...

  2. attr 和 prop的区别

    attr 返回的是字符串 prop 返回的是布尔值

  3. Linux下更新git版本

    查看git版本,卸载旧版本(如果没有安装git请直接到下一步) git --version yum remove git 安装依赖软件 yum install curl-devel expat-dev ...

  4. Linux 字符设备驱动—— ioremap() 函数解析

    一. ioremap() 函数基础概念 几乎每一种外设都是通过读写设备上的相关寄存器来进行的,通常包括控制寄存器.状态寄存器和数据寄存器三大类,外设的寄存器通常被连续地编址.根据CPU体系结构的不同, ...

  5. angularjs与vue循环数组对象是区别

    一直都觉得angularjs和vue是想类似的,今天在限制加载的数据条数时发现 其不同,话不多说,直接看代码: 1.angularjs <li ng-repeat="item in d ...

  6. Button控件双击事件不执行

    Button控件双击事件不执行 最近组态组新加入了一位同事,该同事原先是做ASP.NET的,没有做过WindowForm的开发.所以就先安排一个小示例来测验一下.这个示例有些年头了,当年我进入组态开发 ...

  7. 【转】Excel-VBA操作文件四大方法之三

    三.利用FileSystemObject对象来处理文件 FileSystemObject对象模型,是微软提供的专门用来访问计算机文件系统的,具有大量的属性.方法和事件.其使用面向对象的“object. ...

  8. VS.NET2013发布网站的时候去掉.cs文件(预编译)(转)

      在要发布的网站上右键,选择"发布网站".   在发布窗口中,会让你选择一个发布配置文件,没有的话点击下拉菜单在里面选择新建一个. NEXT.   好,现在发布一下网站.发布出来 ...

  9. 2016-2017-2 20155227实验二《Java面向对象程序设计》实验报告

    2016-2017-2 20155227实验二<Java面向对象程序设计>实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉 ...

  10. 20155230 实验二《Java面向对象程序设计》实验报告

    20155230 实验二<Java面向对象程序设计>实验报告 一.单元测试 三种代码 知道了伪代码.产品代码.测试代码的关系和用途,并根据老师的例子,按测试代码调试了产品代码. 值得注意的 ...