zoukankan      html  css  js  c++  java
  • Kattis

    Kattis - cokolada【水】

    这里写图片描述

    题意

    有一个人想吃巧克力,但是巧克力都是按照 2 的幂次的数量包装的,然后他想吃一定数量块的巧克力,然后可以敲碎,每次敲碎都分成两半,比如四块装的分成两块就是二块和二块,但是只能买一块,求要买的那块的数量,和要敲几次

    思路

    因为数据比较小,可以先打一个 2 的幂次的表,然后二分查找最少的那个数量,然后每次都剪掉一半,然后往下找,直到减为0 就跳出 然后用 大的减最小的那个数 就可以了

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    #include <cstdlib>
    #include <ctype.h>
    #include <numeric>
    #include <sstream>
    using namespace std;
    
    typedef long long LL;
    const double PI  = 3.14159265358979323846264338327;
    const double E   = 2.718281828459;  
    const double eps = 1e-6;
    const int MAXN   = 0x3f3f3f3f;
    const int MINN   = 0xc0c0c0c0;
    const int maxn   = 1e5 + 5; 
    const int MOD    = 1e9 + 7;
    int vis[21];
    int k, ans, flag;
    
    int main()
    {
        for (int i = 0; i < 21; i++)
            vis[i] = 1 << i;
        cin >> k;
        for (int i = 0; i < 21; i++)
        {
            if (vis[i] >= k)
            {
                ans = vis[i];
                flag = i;
                break;
            }
        }
        if (vis[flag] == k)
            printf("%d %d
    ", ans, 0);
        else if (k % 2)
            printf("%d %d
    ", ans, flag);
        else
        {
            k -= ans/2;
            int i;
            for (i = flag; i >= 0; i--)
            {
                if (k >= vis[i])
                    k -= vis[i];
                if (k == 0)
                    break;
            }
            printf("%d %d
    ", ans, flag - i);
        }
    }
    
  • 相关阅读:
    C++基础知识(二)
    C++基础知识(一)
    RT-thread 设备驱动组件之IIC总线设备
    RT-thread 设备驱动组件之SPI设备
    RT thread 设备驱动组件之USART设备
    RT-thread 设备驱动组件之PIN设备
    RT-thread finsh组件工作流程
    C语言知识点
    RT-thread main函数分析
    堆和栈的区别
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433369.html
Copyright © 2011-2022 走看看