Some good questions about binary

P1062 [NOIP2006 Popularization Group] Sequence

#include <bits/stdc++.h>
using namespace std;
int k, n, a[10], cnt;
long long ans;
int main()
{
	scanf("%d %d", &k, &n);
	//将n转换为二进制数, 最低位存在a[0] 
	while(n){
		a[cnt]=n%2;
		cnt++;
		n>>=1;
	}
	for(int i=0; i<cnt; ++i){
		if(a[i]){
			ans+=pow(k, i);	
		}
	}
	printf("%lld", ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/u013313909/article/details/129770368