洛谷 B3619 10进制转x进制
题目描述
给定一个十进制整数 n 和一个小整数 x。将整数 n 转为 x 进制。对于超过十进制的数码,用 A
,B
… 表示。
输入输出案例
进制转换的操作
就是取每个进制的余数,比如取得是二进制那么就是一直除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){ 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进制转换成十进制
这里有个数学叫秦九韶算式多用于多项式操作和将各种进制转换成十进制
然后再解题的时候给出的是一个字符串,然后字符串的第一位实际上是最高位,所有对于这个就是通过一个乘法和加法的循环不断操作。
代码
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; }
|