UVa 11121 Base-2 (number theory &-2 progressive & complementary Ideas)

Source: Internet
Author: User
Tags array cas integer printf time limit

11121-base-2
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=2062

The creator of the universe works in mysterious ways. But
He uses a base ten counting system and likes round numbers.

Scott Adams

Everyone knows about base-2 (binary) integers and base-10 (decimal) integers, but what about base-2? An integer n written in Base-2 is a sequence of digits (BI), writen right-to-left. Each of the which is either 0 or 1 (no negative digits!), and the following equality must.

n = B0 + B1 ( -2) + b2 ( -2) 2 + b3 (-2) 3 + ...

The cool thing is this every integer (including the negative ones) has a unique base--2 representation with no minus Required. Your task is to find this representation.

Input
The "a" of input gives the number of cases, N (at most 10000). N test Cases follow. Each one was a line containing a decimal integer in the range from-1,000,000,000 to 1,000,000,000.

Output
For each test case, output one line containing ' case #x: ' followed by the same integer and written in base-2 with no leading Zeros.
Sample input Output for sample input

4

1

7

-2

0

Case #1:1

Case #2:11011

Case #3:10

Case #4:0

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Ideas:

One idea is: take what we do in the binary system, just make a small change on the line.

Another kind of thinking is a bit ingenious: if the binary corresponds to that one bit P is 1, then n+=1<< (p+1) and finally n into the normal binary. (faster)

Way 1:

/*0.045s*/
    
#include <cstdio>  
    
int array[40];  
    
int main ()  
{  
    int T, N, I, J, case =0 0;  
    scanf ("%d", &t);  
    while (t--)  
    {  
        scanf ("%d", &n);  
        printf ("Case #%d:", ++case);  
        if (n = = 0)  puts ("0");  
        else
        {  
            i = 0;  
            while (n)  
            {  
                array[i++] = n & 1;  
                n = (n-(n & 1))/-2;  
            }  
            for (j = i-1 J >= 0; j--)  
                printf ("%d", array[j]);  
            Putchar (a);  
        }}}  

Way 2:

/*0.019s*/#include <cstdio> #include <cstdlib> typedef long Long LL;  
    
CONST LL one = 1;  
    int main () {ll n, bit;///very unfortunately needs 1<<32, so long int T, cas = 0, p;  
    scanf ("%d", &t);  
        while (t--) {scanf ("%lld", &n);  
        printf ("Case #%d:", ++cas);  
            if (n = = 0) {puts ("0");  
        Continue  
        ///n is converted to n p = (n > 0 1:0) in the sense of 2,///start n = ABS (n);  
        bit = one << p;  
            while (P <=) {if (bit & N) n + = one << (p + 1);///correction  
            p = 2;  
        bit = one << p; ///to the leading 0 for (bit = one << 31; (bit & N) = = 0;  
        Bit >>= 1); Find binary while (bit) {Putchar (bit & n?)  
            ' 1 ': ' 0 ');  
        Bit >>= 1;  
        }Putchar (10);  
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.