Algorithm training-based conversion

Source: Internet
Author: User

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
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}

loseinto the format 
A row of two numbers, the first is the decimal number n ( -32768<=n<=32767), and the second is the base-r of the negative number.

loseOut format 
The output of the negative number and its base, if the base is more than 10, the reference to 16 binary processing method. (Format reference example)

SampleExampleInput 1
30000-2 Sample Output 30000=11011010101110000 (base-2) sample input-20000-2 sample output -20000=1111011000100000 (base-2) sample input 28800-16 sample Output 28800 =19180 (base-16) sample input-25000-16 sample output -25000=7fb8 (base-16) ideas, the division can be, to pay attention to determine the remainder of the positive and negative properties and and-(+) 10 of the relationship Press test instructions, using the short type to define n is completely possible, But one of the test data is out of range and has to use INT
#include <stdio.h>Charstack[ -]; Shorttop =-1; ShortIsEmpty () {if(Top = =-1)        return 0; return 1;}voidPushChare) {stack[++top] =e;}voidPopChar*e) {    *e = stack[top--];}intMain () {intN,temp,r; Chare; scanf ("%d%d",&n,&R); Temp=N; if(-R <=9)    {         while(temp) {if(Temp%r >=0) {push ('0'+temp%R); Temp/=R; }            Else{push ('0'+ (temp-r* (temp/r+1))); Temp= temp/r +1; }        }    }    Else{         while(temp) {if(Temp%r >=0)            {                if(Temp%r <=9) {push ('0'+temp%s); }                Else{//When r=-20, the remainder may be tenPush'A'+ (temp%r-Ten)); } temp/=R; }            Else{                if(-R + temp%r <=9)//Judging the difference in valuePush'0'+ (temp-r* (temp/r+1))); ElsePush ('A'+ (-r + temp%r-Ten)); Temp= temp/r +1; }}} printf ("%d=", N);  while(IsEmpty ()) {pop (&e);    Putchar (e); } printf ("(base%d) \ n", R); return 0;}

Algorithm training-based 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.