[NOIP] hex conversion

[NOIP] hex conversion

topic

Title Description

We can use this manner to represent a decimal number: multiply each digit of the digit to a location as an index, in 1010 and raised to the power of the form. 123 may be expressed as, for example, 1 × 10 ^ 2 2 × 10 1 + 3 × 10 ^ + ^ 0 such form.

Similarly, the binary numbers, it can be expressed as a binary number is multiplied by each of the number to a location as an index, and 2 raised to the power of the form.

Generally, any positive integer or a negative integer R -R may be selected as a radix number systems. If R is -R or as the base, it is necessary to use digital 0,1, ... R-1.

For example, when R = 7, are required to use the digital 0,1,2,3,4,5,6, which, regardless of its R or -R. If the absolute value exceeds 10 Examples of the base, then in order to show these digital commonly used letters to represent those digital greater than 9. For example, in hexadecimal, represented by A 10, 11 represented by B, with a C represents 12, and so on.

In the negative hexadecimal number is used as the base -R, e.g. -15 (decimal) corresponding to 110001 (decimal -2), and it can be expressed as a power series of 2:

110001=1×(−2) 5 +1×(−2) 4 +0×(−2) 3 +0×(−2) 2 +0×(−2) 1 +1×(−2) 0

Design a program that reads a decimal number and a negative number of decimal radix, and this decimal number to hexadecimal To do this under a negative number.

Input Format

Each row has two inputs the input data.

The first is a decimal number n. The second number is the negative of the binary radix -R.

Output Format

This output and the negative hexadecimal base, if this base exceeds 10, the manner of hexadecimal reference process.

Sample input and output

Input # 1 replication
30000-2
output copy # 1
30000 = 11011010101110000 (base-2)

Copy Input # 2
-20000-2
output copy # 2
-20000 = 1111011000100000 (base-2)

Copy Input # 3
28800-16
output copy # 3
28800 = 19180 (base-16)

Copy Input # 4
-25000-16
output copy # 4
-25000 = 7FB8 (base-16)

Description / Tips

[Data] range
to 100% of the data, -20≤R≤-2, |n|≤37336.

NOIp2000 improve the group first question

analysis

This question, just a few decimal numbers into binary numbers and then output is about the same. But there are different, when the number is negative calculation methods and normal is not the same. .
Negative binary: see example -15 / -2 = 7, the remainder 1; (black three questions is not equal to -2 * 7 -14 -15 it is not bigger than you, but also how the remainder 1????)
( Here negative into such a system is, the remainder will not be negative , because the -15 + (-2), then / 2, so that -17 / -2 = 8, I 1,
and then continue 8, in addition to each to be negative, then the first plus the divisor, again divided by the divisor.)

Then how do we write this case, if we restore, is how to restore? Dividend = quotient * divisor + remainder.
Transform the original equation ,, (List 1) divisor + (remainder - divisor) = List divisor + remainder.
Since the remainder can never be greater than the divisor, so to do so, the remainder will always be positive. Problems get. .

Code

#include<iostream>
#include<cstdio>
using namespace std;

int n,r;

void f(int n,int r){
	if(n==0) return;
	
	int m = n%r;
	
	if(m<0){
		n += r;
		m -= r;
	}
	
	if(m>=10){
		m = 'A' + m -10;
	}else{
		m = '0' + m;
	}
	f(n/r,r);
	
	printf("%c",m);						//求二进制是倒序输出的,
	return ;
}
int main(){
	cin>>n>>r;
	cout<<n<<"=";
	f(n,r);
	cout<<"(base"<<r<<")"<<endl;


	return 0;
}
Published 75 original articles · won praise 1 · views 3644

Guess you like

Origin blog.csdn.net/A793488316/article/details/104645913