Dual horsetail

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 500    Accepted Submission(s): 189

Problem Description
  Nanaya's favorite Dual horsetail robot has run out of power!
  As a best friend of Nanaya, Cdfpysw decide to do something for him.
  Cdfpysw, the king of alpaca, has some very powerful energy balls numbered by 1,2,3……n. He let Nanaya divide them into some sets to save the robot.
  However, if there are three balls whose numbers are labeled X,Y and X&Y in the same set, they will explode! Nanaya must divide these balls into some sets and avoid explosion.
  Nanaya is a shy man, he don't want to divide these balls into too many sets. Please tell him the minimum number of sets.
 
Input
  The first line contains an integer T, indicating the number of cases.
  The next T line, each line contains an integer n indicating the number of balls. (1 <= n <= 109)
 
Output
  For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y represents the minimum number of sets.
 
Sample Input
2
2
3
 
Sample Output
Case #1: 1
Case #2: 2
 
 
算法与题目分析:T组数据,每组输入一个n,代表1到n的数据。现在要把这n个数放到一些集合里,求最少的集合数目。
必须满足条件:x, y和(x&y),这三个元素不会同时出现在一个集合了!
      先需要知道一些特殊数字的二进制写法:
      十进制----------二进制
            1               0001
            3               0011
            7               0111
            15             1111
            31          0001 1111
            ......
            规律是:我们从1开始处理数据,当出现一个新的数字,而这个数字的二进制表示全都是1(忽略前导0),就
     必须把这个数字放到一个新增加的集合里面,否则这个数字会与前面的某些数字必定不会满足要求条件。
            n的范围(1 <= n <= 109),所以只需要找出所有的二进制表示全为1的数字的个数。注意不需要把1到n的数据
    挨着进行进制转换。我们只需要进行一个循环即可。二进制表示全为1的这些数是有规律的,如果你了解二进制转十进制
    的算法就会懂的!
            例如:1 = 2^0.
                    3 = 2^0+2^1.
                    7 = 2^2+2^1+2^0.
    代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <algorithm>
#define PI acos(-1.0)
#define INF 1e9 using namespace std; int dd[100];
int main()
{
int t;
scanf("%d", &t);
int n;
memset(dd, 0, sizeof(dd)); int ans;
int e=1;
dd[e++]=1; ans=1;
int i=1;
while(ans < INF )
{
ans=ans+pow(2, i);
i++;
dd[e++]=ans;
}
// printf("-----%d\n", i); int cnt=1;
while(t--)
{
scanf("%d", &n);
int pos;
for(i=0; i<e; i++)
{
if(n>=dd[i] && n<dd[i+1])
{
pos=i; break;
}
}
printf("Case #%d: %d\n", cnt++, pos );
}
return 0;
}

最新文章

  1. 掌握Thinkphp3.2.0----模版基础
  2. Android使用layer-list实现三面边框
  3. K2上海总部技术培训分享笔记
  4. [Flex] ButtonBar系列——皮肤和外观设置
  5. 开发流程习惯的养成—TFS简单使用
  6. event事件:
  7. Unity3d shader之SWAP Force Depth-of-Field Shader
  8. [cocos2dx-lua]&amp;quot;Hello Lua&amp;quot;分析
  9. React的学习(上)
  10. UNIX网络编程——TCP长连接与短连接的区别
  11. 3D数学基础(三)矩阵
  12. HR_Sherlock and Anagrams_TIMEOUT[UNDONE]
  13. struct/class等内存字节对齐问题详解
  14. ss客户端以及tcp,udp,dns代理ss-tproxy在线安装版--centos7.3 x64以上(7.3-7.6x64测试通过)
  15. 【Django】【Shell】
  16. js - object.assign 以及浅、深拷贝
  17. Nginx缓存使用官方教程及常见问题解答
  18. Centos 7中的网卡一致性命名规则
  19. CURL命令测试网站打开速度
  20. 【线段树维护复杂状态】Ryuji doesn&#39;t want to study

热门文章

  1. 百度 api 测试 &amp; python
  2. 如何Enable FireFox里的Java Plugin
  3. windows的iis做后门,隐藏访问,无日志
  4. Hive命令详解
  5. C# 调用API接口处理公共类 自带JSON实体互转类
  6. DevOps 初学者的入门指南
  7. HDOJ2084数塔问题
  8. VMware厚置备延迟置零,厚置备置零,精简置备具体解释
  9. debian jessie install note
  10. FreeRTOS在神舟IV号开发板的应用demo