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?

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

2510

Output

12

题目意思:将一个2进制的n中每个位翻转得到一个比n小且尽可能大的数,求输出翻转了几位。 

解题思路:这道题该由我背锅,我当时先是翻译错了题意,后来稍微有一点眉目了,我又理解错了那个flip的意思,这里面的翻转并不是那种交换(swap那样的),而是像硬币正面换到反面那样的翻转,也就是0与1的交换,根据题意可以推出想要得到一个既要比n小还有尽可能大的数,只有是n前面的那一个数n-1。所以就是根据n构造一个二进制的n-1,方法就是找到n的二进制中最后面的那一个1翻转为0,而最后一个1之后的0全都翻转成1,统计所用的翻转次数即可。
 1 #include<cstdio>
 2 #include<cstring>
 3 int main()
 4 {
 5     int t,n,j,k,i,count;
 6     int a[32];
 7     scanf("%d",&t);
 8     while(t--)
 9     {
10         scanf("%d",&n);
11         memset(a,-1,sizeof(a));
12         j=0;
13         count=0;
14         i=n;
15         while(i)
16         {
17             a[j]=i%2;
18             if(a[j]==0)
19             {
20                 count++;
21             }
22              if(a[j]==1)
23              {
24                  count++;
25                  break;
26              }
27             i/=2;
28             j++;
29         }
30         printf("%d\n",count);
31     }
32     return 0;
33 }


原文地址:https://www.cnblogs.com/wkfvawl/p/9383334.html

时间: 2024-10-13 05:53:23

Flip the Bits(思维)的相关文章

[arc081] F - Flip and Rectangles——思维题+单调栈

题目大意: 给定一个\(n\times m\)的01矩形,每次可以翻转一行或者翻转一列. 求翻转若干次之后的最大全1子矩形. 思路: 首先我们要知道一个结论:如果一个子矩形可以被翻转成为全1矩形,那么它内部的每一个\(2\times 2\)的子矩形的1的个数为偶数. 如果存在一个\(2\times 2\)的子矩形有奇数个1,那么无论怎么操作都还是奇数. 如果所有的\(2\times 2\)的子矩形都有偶数个1,我们可以先使这个矩形的第一行第一列都变为1,根据奇偶性不难发现整个矩阵此时必定全部都变

[leetcode-476-Number Complement]

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note:The given integer is guaranteed to fit within the range of a 32-bit signed integer.You could assume no leading zero

LeetCode 476. Number Complement(easy难度c++)

题目: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leadin

C++ Bitsets

C++ Bitsets给程序员提供一种位集合的数据结构.Bitsets使用许多二元操作符,比如逻辑和,或等. Constructors 创建新bitsets Operators 比较和赋值bitsets any() 如果有任何一个位被设置就返回true count() 返回被设置的位的个数 flip() 反转bits中的位 none() 如果没有位被设置则返回true reset() 清空所有位 set() 设置位 size() 返回可以容纳的位的个数 test() 返回指定位的状态 to_st

LeetCode解题思路:476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

LeetCode 476. Number Complement (数的补数)

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

476. Number Complement【位运算】

2017/3/14 15:36:44 Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could a

476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

The Aggregate Magic Algorithms

http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that create and collect algorithms of all types (here are a few WWW sites). Unfortunately, in building systems hardware and software, we in The Aggregate o