进制转换

洛谷 B3619 10进制转x进制

题目描述

给定一个十进制整数 n 和一个小整数 x。将整数 n 转为 x 进制。对于超过十进制的数码,用 AB … 表示。

输入输出案例

1
2
1000
2
1
1111101000
进制转换的操作
进位制 - OI Wiki (oi-wiki.org)
就是取每个进制的余数,比如取得是二进制那么就是一直除2,然后得到余数,然后余数从下往上读。直到不能再除。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
using namespace std;
typedef long long ll;
constexpr int N = 1e5+10;
int n,x;
void solve(int k){//这里就是用递归的方法
if(k>=x){
solve(k/x);
k%=x;//得到取余值
if(k>=10){//大于10就要考虑A这些字母的东西
cout<<char(k-10+'A');
}
else{
cout<<k;
}

}
else{//不需要递归操作。
if(k>=10){
cout<<char(k-10+'A');
return;

}
else{
cout<<k;
return;
}
}
}

int main(){
cin>>n>>x;
solve(n);
return 0;
}

B3620 x进制转换成十进制

这里有个数学叫秦九韶算式多用于多项式操作和将各种进制转换成十进制
对于秦九韶公式秦九韶算法_秦九韶算法公式详解-CSDN博客看完就知道原里。
然后再解题的时候给出的是一个字符串,然后字符串的第一位实际上是最高位,所有对于这个就是通过一个乘法和加法的循环不断操作。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
```cpp
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
using namespace std;
typedef long long ll;
constexpr int N = 1e5+10;
int n;
string s1;
void solve(string s1,int x){
int res = 0;
for(auto c:s1){
if(c<'A'){//就是没超过是
res = res*x+c-'0';//就是不断的×和+
}
else{
res = res*x+c-'A'+10;
}
}
cout<<res;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
cin>>s1;
solve(s1,n);
return 0;
}


进制转换
https://ljw030710.github.io/2024/02/03/进制转换/
Author
iolzyy
Posted on
February 3, 2024
Licensed under