这道题,我想的太复杂了。= =
我定义的数组存放的是输入数的二进制,但是更好的方法是定义一个数组存放转换成二进制后1的所在位置。
这样就简单了吗。
#include <iostream>
using namespace std;
int main(){
	void handle(int n);
	int n;
	cin>>n;
	handle(n);
	cout<<endl;
	return 0;
}
void handle(int n){
	int num[101];
	int i=0,j=0;
	while(n){
		int temp;
		temp=n%2;
		if(temp==1){
			num[j]=i;			
			j++;
		}
		i++;
		n=n/2;
	}
	for(i=j-1;i>=0;i--){
		if(num[i]==0)
			cout<<"2(0)";
		else if(num[i]==1)
			cout<<"2";
		else if(num[i]==2)
			cout<<"2(2)";
		else{
			cout<<"2(";
			handle(num[i]);
			cout<<")";
		}
		if(i!=0)
			cout<<"+";			
	}
}
原文:http://blog.csdn.net/sunshumin/article/details/44570469