zoukankan      html  css  js  c++  java
  • 二进制的前导的零 【补码】

    7-1 二进制的前导的零(10 分)

    计算机内部用二进制来表达所有的值。一个十进制的数字,比如18,在一个32位的计算机内部被表达为00000000000000000000000000011000。可以看到,从左边数过来,在第一个1之前,有27个0。我们把这些0称作前导的零。

    现在,你的任务是写一个程序,输入一个整数,输出在32位表达下它前导的零的个数。
    输入格式:

    一个整数,在32位的整数可以表达的范围内。
    输出格式:

    一个整数,表达输入被表达为一个32位的二进制数时,在第一个1之前的0的数量。
    输入样例:

    256

    输出样例:

    23

    思路
    打一个二进制的表 然后 lower_bound 判断 第一个 大于等于的数 在第几位
    然后输出 32 - 其数组下标
    坑点:
    如果是负数的话 是补码表示
    负数的补码 第一位是1
    所以 如果是负数 直接输出 0

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-6;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e6 + 5;
    const int MOD = 1e9 + 7;
    
    ll dp[33] = { 1 };
    
    int main()
    {
        for (int i = 1; i < 33; i++)
            dp[i] = dp[i - 1] << 1;
        ll n;
        cin >> n;
        if (n < 0)
            cout << 0 << endl;
        else
        {
            int vis = upper_bound(dp, dp + 32, n) - dp;
            cout << 32 - vis << endl;
        }
    }
  • 相关阅读:
    linux -- Ubuntu 安装搜狗输入法
    linux -- Ubuntuserver图形界面下安装、配置lampp、phpmyadmin
    linux -- ubuntu14.10安装gnome桌面环境和dock工具
    linux -- Ubuntu Server 安装图形界面
    linux -- Ubuntu 命令技巧合集
    linux -- ubuntu 何为软件源
    linux -- Ubuntu报错“unable to locate package...”
    linux -- Ubuntu图形界面终端实现注销、关机、重启
    php -- 读取文本文件内容
    NEXTDAY
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433226.html
Copyright © 2011-2022 走看看