A typical problem of of on bit Fetching and Base Conversion of C++ Read Numbers

  • 2020-06-23 01:40:19
  • OfStack

In this blog, I will not write a specific question, but a summary of a typical problem - reading Numbers out by bit.

Take the number 12345 for example.

First of all, we're going to take the ones place. Take out like this:

12345/1=12345

10=5. // In order to find the pattern

So we have its ones place. The 10 digits are like this:

12345/10=1234

1234%10=4.

Similarly, the hundreds place:

12345/100=123

123%10=3.

So you see, which bit to take out, you have to divide the original number by the bit name of the first bit, and then modularize 10.

Application:


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
  int a[100];
  int wei = 0;
  int num;
  cin >> num;
  while ((num / (int)pow(10, wei)) != 0)    // The end of the loop is if the number of bits is less than that 1 The number of digits of the secondary divided by 
  {
    a[wei] =(num/(int)pow(10,wei))%10;    // Based on what I've just shown you, I'm going to take you guys out and put them in my array. 
    wei++;  
  }
}

Then there is the question of base conversion. It's the same thing as the bit problem, except that you have to take it out and multiply it by the base power of that one bit.

Application:


long long to10(int jz,int num)// Function: Convert the input number to 10 Into the system  
{  
  long long result=0;
  int wei=0;
  while(num/(int)pow(10,wei)!=0)// Take out the input number by bit  
  {
    result+=pow(jz,wei)*((int)(num/pow(10,wei))%10);// Multiply the number by the corresponding base power  
    wei++;
//    (num/1)%10
//    (num/10)%10
//    (num/100)%10
  }
  return result;
 }

conclusion

The above is the site to introduce C++ reading Numbers by bit out and conversion into the base problem, I hope to help you!


Related articles: