zoukankan      html  css  js  c++  java
  • POJ 3191 The Moronic Cowmpouter(二进制的变形)

    The Moronic Cowmpouter

    Time Limit: 1000MS

     

    Memory Limit: 65536K

    Total Submissions: 2402

     

    Accepted: 1227

    Description

    Inexperienced in the digital arts, the cows tried to build a calculating engine (yes, it's a cowmpouter) using binary numbers (base 2) but instead built one based on base negative 2! They were quite pleased since numbers expressed in base −2 do not have a sign bit. 

    You know number bases have place values that start at 1 (base to the 0 power) and proceed right-to-left to base^1, base^2, and so on. In base −2, the place values are 1, −2, 4, −8, 16, −32, ... (reading from right to left). Thus, counting from 1 goes like this: 1, 110, 111, 100, 101, 11010, 11011, 11000, 11001, and so on. 

    Eerily, negative numbers are also represented with 1's and 0's but no sign. Consider counting from −1 downward: 11, 10, 1101, 1100, 1111, and so on. 

    Please help the cows convert ordinary decimal integers (range -2,000,000,000..2,000,000,000) to their counterpart representation in base −2.

    Input

    Line 1: A single integer to be converted to base −2

    Output

    Line 1: A single integer with no leading zeroes that is the input integer converted to base −2. The value 0 is expressed as 0, with exactly one 0.

    Sample Input

    -13

    Sample Output

    110111

    Hint

    Explanation of the sample: 

    Reading from right-to-left:

    1*1 + 1*-2 + 1*4 + 0*-8 +1*16 + 1*-32 = -13

    Source

    USACO 2006 February Bronze

     

     解题报告:这道题是让求负二进制,先打表储存每位的可以表示的最大值和最小值,在枚举即可;

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int MAX = 33;
    struct node
    {
        __int64 low;
        __int64 high;
    }a[MAX];//存储第i个位置可以表示的最大值和最小值
    int main()
    {
        int i;
        __int64 n;
        a[0].low = 0;
        a[0].high = 1;
        for (i = 1; i < MAX; ++i)
        {
            if (i % 2)//这种位置上是表示的负数
            {
                if (i > 2)
                {
                    a[i].low = -pow(2, i) + a[i - 2].low;
                    a[i].high = a[i - 2].low;
                }
                else//边界处理
                {
                    a[i].low = -pow(2, i);
                    a[i].high = 0;
                }
            }
            else//正数的时候
            {
                a[i].high = pow(2, i) + a[i - 2].high;
                a[i].low = a[i - 2].high;
            }
        }
        scanf("%I64d", &n);
        int temp;//存储二进制的位数
        if (n < 0)
        {
            for (i = 1; i < MAX; i = i + 2)//负数的时候
            {
                if (a[i].low <= n)
                {
                    temp = i;
                    break;
                }
            }
        }
        else
        {
            for (i = 0; i < MAX; i = i + 2)//正数的时候
            {
                if (a[i].high >= n)
                {
                    temp = i;
                    break;
                }
            }
        }
        for (i = temp; i >= 0; i --)
        {
            int flag = 0;
            //把正负数分开主要是因为边界问题正数的时候是a[i].high >= n,而负数的时候是a[i].high > n
            if (a[i].high >= n && n > a[i].low && n > 0)//正数的时候
            {
                printf("1");
                flag = 1;
            }
            else if(a[i].low <= n && a[i].high > n && n < 0)//负数的时候
            {
                printf("1");
                flag = 1;
            }
            else
            {
                printf("0");
            }
            if (flag)
            {
                if (i % 2)//正数的时候
                {
                    n += pow(2, i);
                }
                else//负数的时候
                {
                    n -= pow(2, i);
                }
            }
        }
        printf("\n");
        return 0;
    }
    

      

     

  • 相关阅读:
    STL笔记之【map之总概】
    STL笔记之set
    Effective C++笔记之Item49【了解new-handler的行为】
    明成软件条形码打印设置
    将Excel数据导入到SqlServer及导入时数据类型转换失败解决方案
    远程桌面无法复制粘贴传输文件解决办法
    DELPHI如何读取cxcheckcombobox中的值
    Delphi 插入Excel图片和值
    SQL 查询语句先执行 SELECT?
    Linux之xargs命令传递参数的一个过滤器
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2436305.html
Copyright © 2011-2022 走看看