(算法练习)——问题 1105: 数列

要求:
https://www.dotcpp.com/oj/problem1105.html
emmm这题真的是比较巧妙= =(PS刷这些巧的题,真的有价值么。。)
参考大神的思路,转成二进制的规律来处理
题解:
https://blog.dotcpp.com/a/62970

AC代码:

#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;

int main(){
	int k,N,sum = 0,i = 0;
	scanf("%d %d",&k,&N);
	while(N>0){
		if(N %2 == 1){
			int m = 1;
			for(int j = 1;j<=i;j++){
				m = m *k;
			}
			sum = sum + m;
		}
		i++;
		N = N/2;
	}
	printf("%d",sum);
}
发布了212 篇原创文章 · 获赞 6 · 访问量 6399

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104288285