Solid foundation --P2084 hex conversion

Topic links: https://www.luogu.org/problem/P2084

P2084 hex conversion

Topic background

no

Title Description

Today, Xiao Ming learned to binary conversion, such as (10101) 2, then its decimal representation of the equation is:

1*2^4+0*2^3+1*2^2+0*2^1+1*2^0,

Then you programming, an M-ary number N is converted to decimal representation of the equation.

Note: When the coefficient is 0, the item type to be omitted.

Input Format

Two numbers, M and N, separated by a space.

Output Format

Total row, a decimal representation of the formula.

Sample input and output

Input # 1
2 10101
Output # 1
1*2^4+1*2^2+1*2^0

Description / Tips

To 100% of the data, 1 <M <10, N is the number of bits does not exceed 1000.

Road test sites this problem is to convert basic questions about the N-ary.

About binary conversion, I believe that people who learned computer should understand, but since the purpose of a blog is to record the learning process, solid foundation, that here I was detailed to say about it!

Binary Coded Decimal with an example:

For example: 101011, in fact, the base is a power and 2, i.e. " 1 * 2 ^ 5 + 0 * 2 ^ 4 + 1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 1 * 2 ^ 0 "

Formula is: abcd.efg (2) * 2 = D 0 + 2 * C . 1 + B 2 * 2 + 2 * A . 3 + 2 * E -1 + F * 2 -2 + 2 * G -3 (10)

Fractional part for the time being to say (in fact, the power descending order)


As for the method of binary decimal turn presumably Needless to say it ( which you surely will )

1 while(m!=0)
2 {
3     a[++k]=m%2;
4     m/=2;
5 }

 

*In general:

N-ary transfer of the M-ary method:

· 1 can whole first into binary conversion to M-ary (simple but complex)

1.2. Direct conversion (cumbersome and error-prone easy)

 

This question is so long as you understand the relationship between the binary conversion, it is actually very simple.

 1 #include <cstring>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int n, len;
 7 
 8 string a;
 9 
10 int main()
11 {
12     cin >> n >> a;
13     len = a.size() - 1;
14     int k = 0;
15     for (int i = 0; i <= len; i++)
16         if (a[i] == '0')
17             k++;
18     k = len - k;
19     int tmp = len;
20     for (int i = 0; i <= len; i++)
21     {
22         if (a[i] == '0')
23         {
24             tmp--;
25             continue;
26         }
27         else
28         {
29             cout << a[i] << "*" << n << "^" << tmp;
30             if (k)
31             {
32                 cout << "+";
33                 k--;
34             }
35             tmp--;
36         }
37     }
38     return 0;
39 }

 

Lay a solid foundation, do a good job each question! ! !

Author: Gmax

This article belongs to the author and blog Park total, reproduced, please use the link, please do not reprint the original, Thanks ♪ (· ω ·) Techno

2019-08-10

abcd.efg(2)=d*20+c*21+b*22+a*23+e*2-1+f*2-2+g*2-3(10)

Guess you like

Origin www.cnblogs.com/Gmax/p/11332836.html