zoukankan      html  css  js  c++  java
  • 【STL】NYOJ 412 Same binary weight (bitset)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=412

                    Same binary weight

    时间限制:300 ms  |  内存限制:65535 KB 
    难度:3
     
    描述 

    The binary weight of a positive  integer is the number of 1's in its binary representation.for example,the decmial

    number 1 has a binary weight of 1,and the decimal number 1717 (which is 11010110101 in binary) has a binary weight

    of 7.Give a positive integer N,return the smallest integer greater than N that has the same binary weight as N.N will

    be between 1 and 1000000000,inclusive,the result is guaranteed to fit in a signed 32-bit interget.

     
    输入
    The input has multicases and each case contains a integer N.
    输出
    For each case,output the smallest integer greater than N that has the same binary weight as N.
    样例输入
    1717
    4
    7
    12
    555555
    样例输出
    1718
    8
    11
    17
    555557

    看到问题后没有任何思路,在查看讨论区以后发现是使用STL中的bitset来解决的

    大体看完了bitset的用法

    或许暴力的方法是可行的,每次+1逐一尝试

     1 #define _CRT_SBCURE_NO_DEPRECATE
     2 #include <bitset>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <iostream>
     6 
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     int n;
    12     while(scanf("%d",&n)!=EOF)
    13     {
    14         bitset<32> b(n);
    15         int nlen = b.count();
    16         int ilen;
    17         for(int i = n+1;;i++)
    18         {
    19             bitset<32> bb(i);
    20             ilen = bb.count();
    21             if(nlen == ilen)
    22             {
    23                 cout << i << endl;
    24                 break;
    25             }
    26         }    
    27     }
    28     
    29     return 0;
    30 }

    但是提交后超时了,看来需要找更快的方法,看完别人的题解后来自己找一下规律解决问题

    将十进制转化为二进制

    4 :00000100

    8 :00001000

    7 :00000111

    11:00001011

    12:00001100

    17:00010001

    1717:0011010110101

    1718:0011010110110

    1.从右到左发现的第一个01都变成了10

    2.发现的第一个01的左边是没有变化的

    3.01右边的1都移动到了最右边

    (其实我也不知道怎么发现的这么神奇的规律   emmmm...)

    一道比较神奇的题目

    接下来的工作就简单些了,根据发现的规律来实现代码 

    #define _CRT_SBCURE_NO_DEPRECATE
    #include <bitset>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            bitset<32> b(n);
            int i,coun = 0;
            for(i = 0;i < 32;i++)
            {
                if(b[i] == 1)
                    coun++;
                if(b[i] == 1 && b[i+1] == 0)
                {
                    b[i] = 0;
                    b[i+1] = 1;
                    break;
                }
            }
            int j = i-1;
            for(int i = 0;i <= j;i++)
            {
                if(i < coun-1)
                    b[i] = 1;
                else
                    b[i] = 0;
            }
            cout << b.to_ulong() << endl;
        }
    
        
        return 0;
    }
    文章搬运自我的个人博客http://duny31030.top 原博客为静态博客,因备份丢失无法继续更新,所以又搬运回博客园,可能部分文章阅读体验不好,可以到我的静态博客搜索相同标题查看
  • 相关阅读:
    oracle sql日期比较:
    vs 2008 过期问题
    silverlight带有复选框的列
    SQL 把一张表虚拟成两张表
    timeupdown
    ChildWindow 父窗体交互
    Debian CentOS修改时区
    如何优雅地使用命令行设置windows文件关联
    sql复制表结构,复制表内容语句
    VC6.0 中 添加/取消 块注释的Macro代码
  • 原文地址:https://www.cnblogs.com/duny31030/p/8830762.html
Copyright © 2011-2022 走看看