2021.07.26 P1010 幂次方(数论)

[P1010 NOIP1998 普及组] 幂次方 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

重点:

1.二进制

题意:

用20或21表示一个数为二的多少次方

分析:

递归。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n;
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')w=-1;
		ch=getchar();
	}
	while(ch<='9'&&ch>='0'){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*w;
}
string solve(int x,int len=0,string s=""){
	//cout<<x<<endl;//
	//cout<<"   case 1"<<endl;//
	if(!x)return string("0");
	do{
		//cout<<"    case 2"<<endl;//
		if(x&1){
			string si="";
			if(len==1)si="2";
			else si="2("+solve(len)+")";
			si+=s==""?"":"+";
			s=si+s;
		}
	}while(++len,x>>=1);
	return s;
}
int main(){
	n=read();
	string ans=solve(n);
	cout<<ans;
	return 0;
}
 posted on 2021-07-26 20:56  eleveni  阅读(78)  评论(0)    收藏  举报