• FZU


    先上题目:

    Problem 2062 Suneast & Yayamao

    Accept: 146    Submit: 319
    Time Limit: 1000 mSec    Memory Limit : 32768 KB

     Problem Description

    Yayamao is so cute that people loves it so much.

    Everyone wants to buy Yayamao from Suneast (a business man who sells Yayamao).

    Suneast is a strange business man. He sells Yayamao in a random price from 1, 2, 3, 4, 5…, n.

    Suneast is also a lazy business man. He never looks for a change. But people can’t but Yayamao with a lower price, that say people must pay exact money for Yayamao.

    Now, we want to know how many pieces of money people should bring with to buy a Yayamao with the exactly price.

     Input

    There are multiple test cases. Each test case has an integer n(1<=n<=2147483647) in a single line.

     Output

    For each case, output a single integer in a line indicate the number of pieces of money people should bring with to buy a Yayamao whose price is random from 1 to n.

     Sample Input

    1
    2
    5

     Sample Output

    1
    2
    3

     Hint

    In test case 1: people can bring 1 piece of money: 1

    In test case 2: people can bring 2 pieces of money: (1, 1) or (1, 2)

    In test case 3: people can bring 3 pieces of money: (1, 1, 3) or (1, 2, 2) ….

      题意:给你一个数n,问你至少用多少个数,只用这些数就可以表示1~n的所有数。

      比如5:1=1

           2=1+1

           3=1+2

           4=2+2

             5=1+1+3

      当然可能还有很多的方法来表示1~5的数,不过一定需要三个。

      其实我们可以这样分析,将这个数n化成二进制,假如它有l位,那么对于一个l位的二进制数,我们需要用l位二进制才可以将它表示出来,同时比它小的数,我们可以用不超过l位的数来保存表示它们,换言之,我们可以用2^0,2^1,2^2···2^k···这些数来表示从1~n的数,至于k等于多少,那就需要看n的二进制有多少位了。

      这也是背包问题里面的多重背包的基础。

    上代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #define LL long long
     4 #define MAX 2147483647
     5 using namespace std;
     6 
     7 
     8 int main()
     9 {
    10     int n;
    11     int tot;
    12     //freopen("data.txt","r",stdin);
    13     while(scanf("%d",&n)!=EOF){
    14         tot=0;
    15         while(n){
    16             tot++;
    17             n=n>>1;
    18         }
    19         printf("%d
    ",tot);
    20     }
    21     return 0;
    22 }
    2062

             

  • 相关阅读:
    上帝永远不会问你的十件事
    discuz x1.5 showmessage函数和showDialog函数解析
    人生,没有那么简单…
    Proxy代理对象是如何调用invoke()方法的.
    实现简单的AOP前置后置增强
    浅谈设计模式visitor访问者模式
    了解jsp,这一篇就够了.
    jsp之el表达式jstl标签
    orale数据库.实例.表空间.用户.表
    题解 UVa10892
  • 原文地址:https://www.cnblogs.com/sineatos/p/3581446.html
Copyright © 2020-2023  润新知