寒假每日一题打卡day28——AcWing 428. 数列
【题目描述】
【思路】
N=1 ----> 1 ----> 3^0 N=2----> 10 ----> 3^1 N=3----> 11 ----> 3^0+3^1 N=4----> 100 ----> 3^2 N=5----> 101 ----> 3^0+3^2 N=6----> 110 ----> 3^1+3^2 N=7----> 111 ----> 3^0+3^1+3^2 根据上面式子发现规律,以N为基数的序列的第k个数就是:将第 N 项的 N 对应的二进制表示,转换为以 k 为基底的数即可。
import java.io.*;
public class Main{
public static void main(String args[]) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s [] = bf.readLine().split(" ");
int k = Integer.parseInt(s[0]), n = Integer.parseInt(s[1]);
int res = 0, base = 1;
while(n > 0){
//短除法求出n的二进制表示
//N=7----> 111 ----> 3^0+3^1+3^2
// 1 3
// 1 1
// 1 0
res += n % 2 * base;
base *= k;
n /= 2;
}
System.out.println(res);
}
}
