2021.07.26 P1010 幂次方(数论)

2023-05-13,,

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;
}

2021.07.26 P1010 幂次方(数论)的相关教程结束。

《2021.07.26 P1010 幂次方(数论).doc》

下载本文的Word格式文档,以方便收藏与打印。