'commit'
This commit is contained in:
36
TangDou/LuoGuBook/P1017_Prepare.cpp
Normal file
36
TangDou/LuoGuBook/P1017_Prepare.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 110;
|
||||
int n, r;
|
||||
int a[N], idx;
|
||||
|
||||
/**
|
||||
测试数据:
|
||||
-15 -2
|
||||
|
||||
|
||||
结果 110001
|
||||
*/
|
||||
int main() {
|
||||
cin >> n >> r;
|
||||
cout << n << "=";
|
||||
//数位分离
|
||||
while (n) {
|
||||
a[idx] = n % r; //余数
|
||||
n /= r; //商
|
||||
if (a[idx] < 0) { //余数不能是负的,如果是负的,需要借位
|
||||
a[idx] += (-r); //借位增加(-r)
|
||||
n++; //商++
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
//倒序
|
||||
for (int i = idx - 1; i >= 0; i--)
|
||||
if (a[i] >= 10)
|
||||
printf("%c", (char) ('A' + a[i] - 10));
|
||||
else printf("%d", a[i]);
|
||||
|
||||
cout << "(base" << r << ")";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user