Files
python/C++专题课程/NOIP算法总结/字符串处理/2的幂次/P1010.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

25 lines
830 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>**//万能头文件
using namespace std;//流操作命名空间
//递归
string dfs(int n){
//返回处理后的字符,一直递归直至全部处理完(0,1,2必须特判跳出);
string out="";//存储输出
int k=-1,i=1;bool ff[16];//临时变量;
//
for(int i=0;i<=15;i++){
ff[i]=n&1;n=n>>1;//二进制取位运算,取得所有的为1的二进位表示可以操作
}
for(int i=15;i>=0;i--){
if(ff[i]){//判断是否为1
if(i>=2)out+="2(" + dfs(i) + ")+";//二以上则再次递归指数
if(i==1)out+="2+";//特判1
if(i==0)out+="2(0)+";//特判0
}
}
return out.substr(0,out.size()-1);//返回//去除尾部‘+’
}
int main(){
int n;cin>>n;
cout<<dfs(n);
return 0;
}