Several exploration and development of the problem of the system

Source: Internet
Author: User

Several exploration and development of the problem of the system

by Medalplus

What is the binary here does not repeat the = =, this article focuses on how to convert the binary

    • N-in-turn 10-in system

For example (10001) 2 How to convert to 10 binary?

There is a method called the right to expand the sum 10001 can be expanded to 20*1+21*0+22*0+23*0+24*1=17, which translates to (17) 10

Convert to code as follows:

1 intCHANGE_N_TO_10 (intC[],intK) {//C is an n-binary array2   int Base=k,result=0, index;3    for(index=len_c-1; i>=0; i--){4result+=Base*C[index];5     Base*=K;6   }7   returnresult;8}

Time complexity O (binary length)

    • 10 binary turn n binary

algorithm: Short Division

Time complexity O (√n)

    • Binary conversion of decimals

decimal conversion with integer conversions, the decimal point before the result from the bottom up, the decimal point after the result from the top down

    • Binary conversion of negative numbers

The same integer division, but in addition to the future will have a negative balance of the situation, will WA, so to special, if there is such a situation, the current number should be subtracted from the negative, and the remainder +1

    • Typical examples

Conversion NOIP2000 Increase Group T1

"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*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 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, read the cardinality of a decimal number and a negative number, and convert this decimal number to this negative binary:-r∈{-2,-3,-4,...,-"Input description"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 description"
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. "Analysis"Naked Negative-binary conversion, without any roundabout
1#include <iostream>2#include <cstdio>3#include <cmath>4 using namespacestd;5 6 Const intmaxn=10001;7 8InlineintAbxintx) {returnX>0? x:-x;}9 Ten intx,y,d,c[maxn],i,a,b; One  A intMain () { -      while(cin>>x>>y) { -D=0; theprintf"%d=", x); -          while(x) { -a=x%y; -b=x/y; +             if(a<0){ -a-=y; +b++; A             } atx=b; -c[++d]=A; -         } -          for(i=d;i>=1; i--) -           if(c[i]<Ten) printf ("%d", C[i]); -           Elseprintf"%c",(Char)('A'+c[i]-Ten)); inprintf"(base%d) \ n", y); -     } to     return 0; +}

Medalplus

2015-03-06

Several exploration and development of the problem of the system

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.