5 solutions
-
0
#include<bits/stdc++.h> using namespace std; string s; int n; void f(int x){ s=""; for(int i=0;i<n;i++){ if(x&1){ s[i]='Y'; }else{ s[i]='N'; } x>>=1; } } int main(){ cin>>n; for(int i=0;i<pow(2,n);i++){ f(i); for(int i=n-1;i>=0;i--){ cout<<s[i]; } cout<<endl; } return 0; }
我看到题解里没人这么写(? 都是dfs
-
0
#include <bits/stdc++.h> using namespace std; char b[100]; int n, x = 1; void dfs(int step)//递归 { if(step == n) { for(int i = 1; i <= x;i++) { cout << b[i]; } cout << endl; return; } b[x++]='N'; dfs(step+1); x--; b[x++]='Y'; dfs(step+1); x--; } int main() { cin >> n; dfs(0); return 0; }
-
-2
题意
输出指定位数内的二进制数
思路
我本来是用一般递归,难到吐血。
现在我重生了,我要拿回我的一切我现在奉老师之命,回溯,瞬秒它!
对于第k步,只有
a[k]=0
和a[k]=1
两种状态,对于每种状态上DFS,直接瞬秒!代码
废话不多,上代码!
#include<iostream> using namespace std; int n; bool a[20]; void dfs(int); void print(); int main(){ cin>>n; dfs(1); } void print(){//打印二进制数,用Y、N表示 for(int i=1;i<=n;i++) if(a[i]) cout<<"Y"; else cout<<"N"; cout<<"\n"; } void dfs(int k){//k是步数 if(k==n+1){ print(); return; } a[k]=0;//对于a[k]=0的状态 dfs(k+1); a[k]=1;//对于a[k]=1的状态 dfs(k+1); }
倡议
倡议大家多写我这种优质题解。
像那些没有注释、说明的。要么为了刷RP,要么为了炫耀自己做出来了。AC名单已经可以展示了,就不要到题解装逼。还有,这样刷的RP有什么意义吗?
题解是为了教不会本题的人,不是为了像他么炫耀,让他们自惭的!
我题目做不出来的时候,看题解,什么都看不懂。到我做出来了才看得懂,那有什么意义!所以,我自本学期以来,一直写优质题解。但我也希望,自己,也可以从这种优质题解里学习,而不是看到一堆没有注释的烂题解后,愤愤回题面。
- 1
Information
- ID
- 987
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 4
- Tags
- # Submissions
- 92
- Accepted
- 40
- Uploaded By