Fat brother and Maze are playing a kind of special (hentai) game with a piece of gold of length N where N is an integer. Although, Fat Brother and Maze can buy almost everything in this world (except love) with this gold, but they still think that is not convenient enough. Just like if they would like to buy the moon with M lengths of the gold, the man who sold the moon need to pay back Fat Brother and Maze N-M lengths of the gold, they hope that they could buy everything they can afford without any change. So they decide to cut this gold into pieces. Now Fat Brother and Maze would like to know the number of the pieces they need to cut in order to make them fulfill the requirement. The number of the gold pieces should be as small as possible. The length of each piece of the gold should be an integer after cutting.
The first line of the data is an integer T (1 <= T <= 100), which is the number of the text cases.
Then T cases follow, each case contains an integer N (1 <= N <= 10^9) indicated the length of the gold.
For each case, output the case number first, and then output the number of the gold pieces they need to cut.
1 3Sample Output
Case 1: 2Hint
In the first case, the gold can be cut into 2 pieces with length 1 and 2 in order to buy everything they can afford without change.
题意:t组数据,每组数据,一个数n,表示长度为n的黄金,下面求n至少分成几段,使得 分成的几段能 组成 小于等于n的任意数;
其实:从写出 1~10的分法,就能找到规律了;
其实就是二进制位 也可以说就位二进制就分成几段,8 分成 1,2,4,1 9分成 1,2,4,2 10分成 1,2,4,3 把小于它位数的二进制全部分出(因为 7 的二进制为全为1 ,所以 只能分出 1,2,4,等于他的位数) 如 10 分出的为 1,2,4 ;这时小于等于7的数,全部能组成了,那么大于7的部分呢? 既然你组成 小于等于7的所有数, 那么 让 n - 1~7 中的数一定全部包含 大于 7的数;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 100010
#include<math.h>
#define ll long long
int sum[40];
int main()
{
int i,j;
int t;
for(i = 0;i<=30;i++)
{
if(i == 0)
sum[i] = 1;
else sum[i] =(int)pow(2,i);
}
scanf("%d",&t);
int pp = 1;
while(t--)
{
int n;
scanf("%d",&n);
for(i = 0;i<=30;i++)
{
n -= sum[i];
if(n<=0)
break;
}
printf("Case %d: ",pp++);
printf("%d\n",i+1);
}
return 0;
}