zoukankan      html  css  js  c++  java
  • UVA11645 Bits【位运算+大数】

    A bit is a binary digit, taking a logical value of either “1” or “0” (also referred to as “true” or “false”respectively). And every decimal number has a binary representation which is actually a series of bits.If a bit of a number is “1” and its next bit is also “1” then we can say that the number has a 1 adjacentbit. And you have to find out how many times this scenario occurs for all numbers up to N.

    Examples:

    Number Binary     Adjacent Bits

    12           1100        1

    15           1111        3

    27           11011     2

    Input

    For each test case, you are given an integer number (0 ≤ N ≤ ((263)−2)), as described in the statement.The last test case is followed by a negative integer in a line by itself, denoting the end of input file.

    Output

    For every test case, print a line of the form ‘Case X: Y ’, where X is the serial of output (startingfrom 1) and Y is the cumulative summation of all adjacent bits from 0 to N.

    Sample Input

    0

    6

    15

    20

    21

    22

    -1

    Sample Output

    Case 1: 0

    Case 2: 2

    Case 3: 12

    Case 4: 13

    Case 5: 13

    Case 6: 14


    问题链接UVA11645 Bits

    问题简述:输入正整数n,求0-n中,有多少个连续的11。

    问题分析数学计算方法有点没搞懂,先占个位置。

    程序说明:算出的结果超出了long long的范围,用两个long long存储输出。

    题记:(略)


    AC的C++语言程序如下:

    /* UVA11645 Bits */
    
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    const long long MOD = 1e13;
    
    long long a, b;
    
    inline void add(long long x)
    {
        b += x;
        a += b / MOD;
        b %= MOD;
    }
    
    inline void solve (long long n) {
        long long digit = 1, v = n;
    
        a = b = 0;
    
        while(n) {
            add((n >> 2) * digit);
            if((n & 3) == 3)
                add((v & (digit - 1)) + 1);
            digit <<= 1;
            n >>= 1;
        }
    
        if (a)
            printf("%lld%013lld
    ", a, b);
        else
            printf("%lld
    ", b);
    }
    
    int main()
    {
        long long n;
        int caseno = 0;
    
        while (scanf("%lld", &n) !=EOF && n >= 0) {
            printf("Case %d: ", ++caseno);
            solve(n);
        }
    
        return 0;
    }



  • 相关阅读:
    c#中using System.Runtime.Serialization.Json;不能引用
    VS2013 当前不会命中断点还未为文档加载任何符号
    windows2008 设置会话超时时间
    服务没有及时响应启动或控制请求 1053
    IIS装好了无法访问localhost
    Shiro笔记——简介、 架构分析
    Java 连接使用 Redis
    Java 连接操作 Redis 出现错误
    网络方面的常用命令 & 常用端口介绍
    Redis 配置文件及命令详解
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563618.html
Copyright © 2011-2022 走看看