5 solutions

  • 4
    @ 2024-1-31 20:38:42
    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    int n;
    void fnd(string w){
    	if(w.size()==n) cout<<w<<"\n";
    	else{
    		fnd(w+'N');
    		fnd(w+'Y');
    	}
    }
    int main(){
    	cin>>n;
    	fnd("");
    	return 0;
    }
    

    递归填数秒了!

    • 2
      @ 2024-1-24 18:34:08
      #include<bits/stdc++.h>
      using namespace std;
      int n;
      string a("NY");
      void f(string s){
      	if(s.size()>=n){
      		cout<<s<<endl;
      		return;
      	}
      	for(int i=0;i<a.size();i++){
      			f(s+a[i]);
      	}
      }
      int main(){
          cin>>n;
          f("\0");
      }
      
      • 0
        @ 2025-3-5 16:20:57
        #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
          @ 2024-12-12 22:20:09
          #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
            @ 2024-3-8 18:59:42

            题意

            输出指定位数内的二进制数

            思路

            我本来是用一般递归,难到吐血。

            现在我重生了,我要拿回我的一切

            我现在奉老师之命,回溯,瞬秒它!

            对于第k步,只有a[k]=0a[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有什么意义吗?

            题解是为了教不会本题的人,不是为了像他么炫耀,让他们自惭的!

            我题目做不出来的时候,看题解,什么都看不懂。到我做出来了才看得懂,那有什么意义!所以,我自本学期以来,一直写优质题解。但我也希望,自己,也可以从这种优质题解里学习,而不是看到一堆没有注释的烂题解后,愤愤回题面。

            • @ 2025-3-8 22:00:56

              枚举队列然后打印队列!

              泰裤辣

              Ps:逆天踩数

            • @ 2025-3-8 22:01:55

              但我还是点了个赞

          • 1

          Information

          ID
          987
          Time
          1000ms
          Memory
          128MiB
          Difficulty
          4
          Tags
          # Submissions
          92
          Accepted
          40
          Uploaded By