zoukankan      html  css  js  c++  java
  • 贪心,打表(或者快速幂), UVA

    题目链接:

    https://cn.vjudge.net/problem/34398/origin

    题目比较简单,就是水题,基础贪心,大于所需的即可:

    AC代码:

    打表:

    #include <cmath>
    #include <iostream>
    #include <cstdio>
    #define ll long long
    
    using namespace std;
    const int MX = 50;
    ll mp[MX];
    
    void get_table() //打一个2^n的表即可
    {
        for(int i = 0; i <= 32; ++i)
        {
            mp[i] = pow(2, i);
        }
    }
    
    int main()
    {
        int k = 0;
        get_table();
        ll n;
        while(scanf("%lld", &n) != EOF && (n > 0))
        {
            k++;
            for(int i = 0; i <= 32; ++i)
            {
                if(mp[i] >= n) //判断一下,如果2^i大于等于则足够copy完。
                {
                     printf("Case %d: %d
    ", k, i);
                     break;
                }
    
            }
        }
    }
    View Code

    快速幂:

    #include <iostream>
    #include <cstdio>
    #define ll long long
    
    using namespace std;
    ll poww(ll a, ll b)
    {
        ll ans = 1, base = a;
        while(b)
        {
            if(b&1 != 0)
                ans *= base;
            base *= base;
            b >>= 1;
        }
        return ans;
    }
    
    
    int main()
    {
        int k = 0;
        ll n;
        while(scanf("%lld", &n) != EOF && (n > 0))
        {
            k++;
            //printf("%lld
    ", poww(2, n));
            if(n == 1)
            {
                printf("Case %d: 0
    ", k);
                continue;
            }
            for(int i = 1; i <= 30; ++i)
            {
                if(poww(2, i) >= n)
                {
                    printf("Case %d: %d
    ", k, i);
                    break;
                }
            }
        }
    }
    View Code

    如有疑问,欢迎评论指出!

    化繁为简 大巧不工
  • 相关阅读:
    POJ 1325 Machine Schedule(待整理)
    URAL 1109 Conference
    结构体排序初始化最傻最傻的错误
    Radar Installation
    关于Eclipse中插件的安装和文件导出
    贪心算法概述
    今年暑假不AC(水题)
    100197C
    100722C
    树状数组
  • 原文地址:https://www.cnblogs.com/mpeter/p/10292240.html
Copyright © 2011-2022 走看看