函数应用举例+幂次方表示法

#include<bits/stdc++.h>
//6-6-例2
using namespace std;
int x=0,p[16];
bool first=true;
void npow(int n){
	while(n){
		for(int i=x;i>=0;--i){
		//从接近n值的2的幂次方开始分解
			if(p[i]<=n){
				n=n-p[i];
				if(first)	first=false;	//连续判断两次此程序才会输出"+"
				else	cout<<"+";	//只有在边界之后才会使用"+"
				if(i>1){
					cout<<"2(";
					first=true;	//保证没有到达递归边界都不输出"+"
					npow(i);	//将i分解
					cout<<")";
				}
				if(i==1)	cout<<"2";
				if(i==0)	cout<<"2(0)";
				//递归边界
			}
		}
	}
}
void input(int n){
	//存放2的次方
	p[0]=1;
	while(p[x]<=n){
		++x;
		p[x]=p[x-1]*2;
	}
}

int main(){
	int n;
	cin>>n;
	input(n);
	npow(n);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1211903563/article/details/88620521