NOIP2000 binary Conversion

Source: Internet
Author: User

Conversion (18 points)

Problem 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*102+2*101+3*100.

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 you need to use the digital 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)

0* (-2) 1 +1* (-2) 0

Problem solving

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

input

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

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.

Sample Example

Input

30000-2

-20000-2

28800-16

-25000-16

Output

30000=11011010101110000 (base-2)

-20000=1111011000100000 (base-2)

28000=19180 (base-16)

-25000=7fb8 (base-16)

Ideas

Mathematical.

For the binary to be positive, we only need to record the remainder and then reverse the output.

So for the negative, we can also use this method, for the remainder of the case of negative numbers, we need to borrow, pay attention to the borrowed +1 and itself is-R.

Code

1#include <iostream>2 using namespacestd;3 4 intMain () {5     intN, R;6cin>>n>>r; cout<<n<<"=";7     inta[ -], nc=0;8      while(n) {9a[nc]=n%R;TenN/=R; One         if(a[nc]<0) { AA[nc]-=r; n++; -         } -Nc++; the     } -      for(inti=nc-1; i>=0; i--) -       if(a[i]>=Ten) {CharCh='A'+a[i]-Ten; cout<<ch;} -       Elsecout<<A[i]; +cout<<"(Base"<<r<<")"; -     return 0; +}

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.