B - Digits /
AtCoder - 5308
Problem Statement
Given is an integer N. Find the number of digits that N has in base K.
Notes
For information on base-K representation, see Positional notation - Wikipedia.
Constraints
All values in input are integers.
1≤N≤109
2≤K≤10
Input
Input is given from Standard Input in the following format:
N K
Output
Print the number of digits that N has in base K.
Sample Input 1
Copy
11 2
Sample Output 1
Copy
4
In binary, 11 is represented as 1011.
Sample Input 2
Copy
1010101 10
Sample Output 2
Copy
7
Sample Input 3
Copy
314159265 3
Sample Output 3
Copy
18
这道题的就是求n的k进制
其实和十进制转二进制差不多
即“除K取余,逆序排列”
附代码:
// 转进制函数 #include<bits/stdc++.h> using namespace std; char a[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; void t(int n,int k); int main() { int n,k; cin>>n>>k; t(n,k); } void t(int n,int k){ int r=n%k; n/=k; if(n!=0) t(n,k); cout<<a[r]; }
\\AC代码: #include<bits/stdc++.h> using namespace std; char a[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int cnt =0; void t(int n,int k); int main() { int n,k; cin>>n>>k; t(n,k); cout<<cnt<<endl; } void t(int n,int k){ int r = n%k; n /= k; if(n!=0) t(n,k); cnt++; }