蓝桥杯算法训练 2的次幂表示

情到浓时终转凉″ 提交于 2019-12-17 00:42:22

如果可以,可以陪你千年不老,千年只想眷顾你倾城一笑;如果愿意,愿意陪你永世不离,永世只愿留恋你青丝白衣。

#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n;
int dg(int x){
	int ct=0,j=2,sup;
	if(x==2)
		cout<<"2";//如果是二直接输出 
	else{//不是二肯定还能分解 
		cout<<"2";
		if(x==1)//若是一,即2的0次方 
			cout<<"(0)";
		else{
			while(x/j>0){ 
				ct++;//最高位位数 
				j*=2;
			}
			sup=x%(j/2);//余数 
			if(ct!=1){
				cout<<'(';
				dg(ct);
				cout<<')';
			}
			if(sup>0){//有余数接着分解 
				cout<<'+';
				dg(sup);
			}
		}
	}
}
int main(){
	cin>>n;
	dg(n);
	return 0;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!