'commit'
This commit is contained in:
39
TangDou/CSP-J/J1_2020/3_0.cpp
Normal file
39
TangDou/CSP-J/J1_2020/3_0.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 代码的作用是十进制的n转换成k进制的数字,len为结果的长度
|
||||
const int N = 10010;
|
||||
int n; // 要转换的10进制数
|
||||
int k; // k进制
|
||||
int len = 1; // 最少是1位长度,目前转换到的长度
|
||||
int d[N]; // 保留结果的数组
|
||||
|
||||
int main() {
|
||||
cin >> n >> k;
|
||||
|
||||
//从0开始枚举每小于n的数字,一个个叠加上去
|
||||
for (int i = 0; i < n; i++) {
|
||||
++d[0]; //将叠加上来的1先放到个位上
|
||||
|
||||
//已有的每一个数位都有可能产生进位,注意从下标从0开始
|
||||
for (int j = 0; j < len - 1; j++)
|
||||
if (d[j] == k) {
|
||||
d[j] = 0;
|
||||
d[j + 1] += 1;
|
||||
}
|
||||
|
||||
//最高位单独处理
|
||||
if (d[len - 1] == k) {
|
||||
d[len - 1] = 0;
|
||||
d[len] = 1;
|
||||
++len;
|
||||
}
|
||||
}
|
||||
|
||||
//输出k进制的表示法
|
||||
for (int i = len - 1; i >= 0; i--) cout << d[i];
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user