I. Basics of Conversion
1What's a decimal?
The progressive system, also known as the rounded counting system, isArtificially defined counting methods with rounding. For any of the binary systems X, it means that the numbers in each position operate one place to the Xth power. In decimal, it's one to the nearest ten.hexadecimalis rounded up to the nearest sixteen, binary is rounded up to the nearest two, and so on, and x-ary is rounded up to the nearest x.
2What are some of the most common formulas in life?
Decimal, 60, 12, 24, and so on;
3How do you count in n-ary?
In decimal: 0 1 2 3 4 5 6 7 8 9 10 11 ......
2Progressive system:0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000……
8Progressions: 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 ......
16Progressive system:0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20……
II. Conversion from decimal to R
1、Convert a decimal integer to an R-integer. Divide R by the remainder method。☆☆☆☆☆
2, study skills:You can refer to the short division method, the role of the short division method is used to split out each bit of the decimal n, can be understood as decimal to decimal, the use of the method is to divide by 10 to get the balance, and then inverted;
Then the conversion of decimal to R is, naturally, dividing by R to get the remainder, and then reversing it;
III. Conversion from R to decimal system
1、RConversion to decimal integers. unfolding as a matter of course。
Unfolding by right:The base number is N, as long as the numbers and its power multiply, the product is added, and the sum is a decimal number.
2. Study skills: It can be learned by referring to the decimal integer computer system;
12345=5 * 1 + 4 * 10 + 3 * 100 + 2 * 1000 + 1 * 10000
=5 * 100 + 4 * 101 + 3 * 102 + 2 * 103 + 1 * 104
Four,10Progressive and R-conversion program implementation
Converting a positive integer N to a binary number
Title Description
Enter an integer n not greater than 32767 and convert it to a binary number.
importation
The input consists of one line, including an integer n (0<=n<=32767)
exports
The output is only one line.
example
importation
100
exports
1100100
Defining stringsStore N-converted binary numbers
Divide by 2 using short division to get the remainder, and store the remainder in reverse order in the string r.
-
#include<bits/stdc++.h>
-
-
using namespace std;
-
-
-
//10 to binary
-
-
int n;
-
-
string r = "";//Store conversion results
-
-
int main(){
-
-
cin>>n;
-
-
//when n!=0 cycle
-
-
while(n != 0){
-
-
//cout<<n%2;//take remainder
-
-
//convert the result of n%2 to the character + in front of r
-
-
r = char(n%2+'0') + r;
-
-
n=n/2;//Divide by 2
-
-
}
-
-
-
-
//Special judgment input of 0
-
-
if(r == "") cout<<0;
-
-
else cout<<r;
-
-
return 0;
-
-
}
Attention:
forced type conversion
int y = 97;
cout<<y<<endl;
//exportsasciicodeyalphanumeric
cout<<char(y)<<endl;
In the topic of conversion, be sure to pay special attention to the inputs for the0The situation.
Convert positive integer n to hexadecimal
Title Description
Please read from the keyboard a non-negative integer n (n is aNot more than 18(a non-negative integer), converts n to hexadecimal!
Note: The hexadecimal system, i.e., every 16 into 1, each bit can be from small to large for 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, a total of 16 different sizes, i.e., every 16 into 1, in which A, B, C, D, E, F, respectively, the six letters to indicate that the 10, 11, 12, 13, 14, 15.
For example, the hexadecimal equivalent of 60 is 3C. (For letters, usebanker's anti-fraud numerals)
importation
A nonnegative integer nnn with no more than 181818 bits.
exports
The hexadecimal value of the number.
example
importation
100000000000
exports
174876E800
Idea: divide by 16 to get the remainder!
Be careful when storing to strings in reverse order:
Integer 0~9, convert to character '0'~'9', x + '0'
Integer 10~15, converted to characters 'A' ~ 'F', x + 'A' - 10
Solution 1: Determine which range of 0~9 and 10~15 the result of n%16 is in, and convert it to the corresponding character.
-
#include<bits/stdc++.h>
-
-
using namespace std;
-
-
-
long long n;
-
-
string r = "";//Store hexadecimal
-
-
-
int main(){
-
-
cin>>n;
-
-
//Special judgment input of 0
-
-
if(n == 0){
-
-
cout<<0;
-
-
return 0;// Stop function
-
-
}
-
-
-
-
//Short division
-
-
while(n != 0){
-
-
//n%16: if between 0~9, convert to characters '0'~'9'
-
-
// If between 10~15, convert to characters 'A'~'F'
-
-
if(n%16 <= 9){
-
-
r = char(n%16+'0') + r;
-
-
}else{
-
-
r = char(n%16+55) + r;
-
-
}
-
-
-
-
n = n / 16;//Divide 16
-
-
}
-
-
-
-
cout<<r;
-
-
return 0;
-
-
}
Solution 2: Simplify by using a string to store the hexadecimal equivalent of the character16The process of converting progressions to characters
-
#include<bits/stdc++.h>
-
-
using namespace std;
-
-
-
long long n;
-
-
string r = "";//Store hexadecimal
-
-
string t = "0123456789ABCDEF";//Store the characters corresponding to 0~15
-
-
-
int main(){
-
-
cin>>n;
-
-
//Special judgment input of 0
-
-
if(n == 0){
-
-
cout<<0;
-
-
return 0;// Stop function
-
-
}
-
-
-
-
//Short division
-
-
while(n != 0){
-
-
//n%16: if between 0~9, convert to characters '0'~'9'
-
-
// If between 10~15, convert to characters 'A'~'F'
-
-
r = t[n%16] + r;
-
-
n = n / 16;//Divide 16
-
-
}
-
-
-
-
cout<<r;
-
-
return 0;
-
-
}
Attention:
intup to231-1,10bit integer;
long longup to263-1,19bit integer;
Binary 10 to D
Title Description
The conversion of the decimal integer N and other D (D values of 2, 8, 16) is a basic problem for computers to realize calculations, and there are many ways to solve it, one of which is a simple algorithm is to divide by D to get the balance, and then invert it to get the number of the D system.
for example:
(1348)10=(10101000100)2
(1348)10=(2504)8
(1000)10=(3E8)16
(Please note: When converting to hexadecimal, use A for remainder 10 and B for remainder 11. ......)
Suppose that a program is to be written that meets the following requirement: for any non-negative decimal integer (n ≤ 1,000,000,000) that is input, print out its equivalent in D.
importation
There are two integers N and D. N represents the decimal non-negative integer to be converted, and D represents the base (2, 8, or 16) to be converted.
exports
The result of converting from N to D.
example
importation
1348 2
exports
10101000100
-
#include <bits/stdc++.h>
-
-
using namespace std;
-
-
-
string t = "0123456789ABCDEF",r = "";
-
-
int n,d;
-
-
int main(){
-
-
cin>>n>>d;
-
-
while(n != 0){
-
-
r = t[n%d] + r;
-
-
n = n / d;
-
-
}
-
-
-
if(r == "") cout<<0;
-
-
else cout<<r;
-
-
return 0;
-
-
}
Binary to decimal conversion
Title Description
Convert a binary positive integer up to 25 bits to decimal!
importation
A binary positive integer up to 25 bits
exports
The decimal equivalent of the number
example
importation
111111111111111111111111
exports
16777215
1 |
1 |
0 |
1 |
|||||||||
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
|
()-1 |
Idea: start at the lowest (()-1) and work backwards (expand by weights)
s[i] – ‘0’
Prepare the variable t to denote the nth power of 2, t = 1 every cycle, t = t * 2
-
#include<bits/stdc++.h>
-
-
using namespace std;
-
-
-
string s;
-
-
int t = 1;// denotes the second power of 2, default is 0 power of 2
-
-
int r = 0;// Indicates the decimal equivalent of binary.
-
-
-
int main() {
-
-
cin>>s;
-
-
//Reverse Expand by right
-
-
for(int i = s.size() - 1;i >= 0;i--){
-
-
//s[i] - '0': get the integer corresponding to the character s[i]
-
-
r = r + (s[i] - '0') * t;
-
-
t = t * 2;
-
-
}
-
-
-
-
cout<<r;
-
-
return 0;
-
-
}
hexadecimal to decimal
Title Description
Please place aNo more than 10The hexadecimal positive integer is converted to a decimal integer.
importation
A positive hexadecimal integer up to 10 digits, and if there is a letter in that hexadecimal, the letter is represented by an uppercase English letter.
exports
The decimal integer corresponding to the number.
example
importation
2ECF
exports
11983
2 |
E |
C |
F |
|||||||||
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
Idea: Calculate in reverse order and expand by weight!
Get each bit s[i] from s is a character to be converted to an actual integer!
s[i]:’0’~‘9’,s[i] – ‘0’
s[i]:’A’~’F’,s[i] – ‘A’ + 10
-
#include<bits/stdc++.h>
-
-
using namespace std;
-
-
-
string s;
-
-
long long r = 0,t = 1;//t indicates weight (16th power)
-
-
-
int main() {
-
-
cin>>s;
-
-
// Loop the string in reverse order, counting from the lowest bit.
-
-
for(int i = s.size() - 1;i >= 0;i--){
-
-
//If the current bit is a numeric character 0~9
-
-
if(isdigit(s[i])){
-
-
r = r + (s[i] - '0') * t;
-
-
}else{
-
-
//If it's a letter 10~15
-
-
r = r + (s[i] - 55) * t;
-
-
}
-
-
-
-
// weighting up
-
-
t = t * 16;
-
-
}
-
-
-
-
cout<<r;
-
-
return 0;
-
-
}
Rei is looking for half of the text.
Title Description
Xiaoli students learned the concept of palindrome in programming, if a number is read positively and negatively is the same number, then this number is a palindrome; for example: 2, 5, 8, 66, 121, 686, 12321 are palindromes, Xiaoli found that such a number is not too many. So Xiaoli has an idea, if the number is not a palindrome, but the number is a palindrome in binary or hexadecimal, even if the integer is half a palindrome, for example, 417 is not a palindrome, but 417 corresponds to the hexadecimal number is 1A1 is a palindrome, so 417 counts as half a palindrome.
Please program to help Rei find the number of half-returns that match the criteria.
importation
The first line is an integer n (10<=n<=100)
The second line is n integers (these are all integers between 0 and 10^8)
exports
The number of all eligible half-returns, one per line.
example
importation
-
5
-
121 417 27 100 21
exports
-
417
-
27
-
21
-
#include<bits/stdc++.h>
-
-
using namespace std;
-
-
-
/*
-
-
Half palindrome: the number x is not a palindrome in decimal.
-
-
In binary or hexadecimal it's an echo.
-
-
*/
-
-
// Determine if integer n is a palindrome in d-digit
-
-
bool huiwen(int n,int d){
-
-
string r = "",t = "0123456789ABCDEF";
-
-
while(n != 0){
-
-
r = t[n%d] + r;
-
-
n = n / d;
-
-
}
-
-
-
// Determine if r is a palindrome
-
-
string r2 = r;
-
-
reverse(r2.begin(),r2.end());
-
-
//cout<<r<<" "<<r2;
-
-
if(r == r2){
-
-
return true;
-
-
}else{
-
-
return false;
-
-
}
-
-
}
-
-
-
int main() {
-
-
int n,x;
-
-
cin>>n;
-
-
for(int i = 1;i <= n;i++){
-
-
cin>>x;
-
-
if(huiwen(x,10)==false&&(huiwen(x,2)==true||huiwen(x,16))){
-
-
cout<<x<<endl;
-
-
}
-
-
}
-
-
return 0;
-
-
}