NOIP2000 binary Conversion

Source: Internet
Author: User

Title Description

We can represent a decimal number in such a way that each Arabic numeral is multiplied by an exponent of the position of the number (minus 1), and the sum of the powers of the base 10. For example: 123 can be expressed as a form of 1*10^2+2*10^1+3*10^0.

Similarly, for a binary number, it can also be expressed as each binary digit multiplied by a position of the number (value-1) as an exponent, with 2 as the sum of the power of the form. Generally, any positive integer R or a negative integer-R can be chosen as the cardinality of a system. If the base is R or-R, then the digital required is 0,1,.... R-1. For example, when r=7, the required digital is 0,1,2,3,4,5 and 6, which is independent of R or-R. If the absolute value of the radix is more than 10, then in order to represent these figures, the English alphabet is usually used to denote numbers greater than 9. For example, for the 16 decimal number, A is used to denote 10, a B to 11, a C for 12, a D for 13, an E for 14, and a F for 15.

In the negative number is the-R as the base, for example, 15 (decimal) is equivalent to 110001 (-2 binary), and it can be represented as a series of 2 and several:

110001=1*(-2)5+1*(-2)4+0*(-2)3+0*(-2)2+0*(-2)1 +1*(-2)0

Design a program that reads the cardinality of a decimal number and a negative binary number and converts this decimal number to this negative number:-r∈{-2,-3,-4,...,-20}

input/output format

Input format:

Each line entered has two input data.

The first is the decimal number n ( -32768<=n<=32767), and the second is the base-r of the negative binary number.

Output format:

The result is displayed on the screen, relative to the input, this negative number and its cardinality should be output, if this base is more than 10, then the method is referred to 16 binary processing.

input/Output sampleInput Sample # #:
30000-2
Sample # # of output:
30000=11011010101110000 (base-2)
Input Sample #:
-20000-2
Output Example #:
-20000=1111011000100000 (base-2)
Input Sample # #:
28800-16
Output Sample # #:
28000=19180 (base-16)
Input Sample # #:
-25000-16
Output Example # #:
-25000=7fb8 (base-16)
Description

The first problem of NOIp2000 raising group

-------------------------------------------------------------------------

Water...................................... Practice in the system

Details: For a certain one is a negative number, R is negative n is going through this step is also negative, similar to the borrow-R, which is changed positive, n minus so + +

#include <iostream>using namespacestd;Const intn=1e5;intn,r,num=-1;intA[n];Charalp[Ten]={'A','B','C','D','E','F','G','H','I'};voidPrintintx) {    if(x<Ten) cout<<x; Elsecout<<alp[x-Ten];}intMain () {CIN>>n>>R; cout<<n<<"=";  while(n!=0) {a[++num]=n%R; N/=0; if(a[num]<0) a[num]-=r,n++; //cout<<a[num]<< "\ n";    }     for(intj=num;j>=0; j--)if(a[j]!=0|| j!=num) print (a[j]); cout<<"(Base"<<r<<")";}

NOIP2000 binary Conversion

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.