//
// Created by heolis on 19-5-3.
//
#include <bits/stdc++.h>
using namespace std;
/*
* P 进制转换为10进制
* */
int P_scale_to_decimalism(int x, int P) {
int y = 0, product = 1;
while (x != 0) {
y = y + (x % 10) * product; // x % 10是为了每次获取x的个位数
x = x / 10; // 去掉个位数
product = product * P;
}
return y;
}
/*
* 10进制转为Q进制
* */
string Decimalism_to_Q_scale(int x, int Q) {
int z[40], num = 0; // 数组z存放Q进制数y的每一位,num为位数
do {
z[num++] = x % Q; //除基取余
x = x / Q;
} while (x != 0);
reverse(z, z + num);
string result;
stringstream ss;
for (int i = 0; i < num; ++i) {
ss << z[i];
}
ss >> result;
return result;
};
int main() {
int n = 10;
cout << P_scale_to_decimalism(n, 2) << endl;
cout << Decimalism_to_Q_scale(n, 2) << endl;
return 0;
}
网友评论