5 solutions

  • 1
    @ 2024-3-17 16:59:21
    #define ll long long
    #include<bits/stdc++.h>
    using namespace std;
    int n;
    void mix(char i,string w){
    	if(int(i-'0')>n) for(int j=0;j<w.size();j++) cout<<int(w[j]-'0')<<' ';
    	else{
    		string t=w;
    		w=t+i+t;
    		mix(i+1,w);
    	}
    }
    int main(){
    	system("color F1");
    	cin>>n;
    	mix('2',"1");
    	return 0;
    }
    

    这题可以直接递归解决

    • 1
      @ 2023-12-30 15:47:22

      一个经典递归题

      递归条件 f(x-1);
               / x==1 -> 1
      递归边界-|
               \ x>1 -> x
      
      #include<iostream>
      using namespace std;
      void f(int x)
      {
      	if(x==1)//为1时返回且输出1
      	{
      		cout<<1<<" ";
      		return;
      	}
      	f(x-1);//左递归
      	cout<<x<<" ";//左边与右边之间输出
      	f(x-1);//右递归
      }
      int main()
      {
      	int n;
      	cin>>n;//输入
      	f(n);//递归
      	return 0;//完结散花
      }
      
      • 1
        @ 2023-12-26 13:12:16

        题意

        f(n)f(n)f(n1),n,f(n1)f(n-1),n,f(n-1)

        f(1)f(1)11

        代码

        #include <bits/stdc++.h>
        using namespace std;
        void f(int n){
        	if (n == 1){	// 递归边界
        		printf("1 ");
        		return ;
        	}
        	f(n - 1);	// sn-1
        	printf("%d ",n);	// n
        	f(n - 1);	// sn-1
        }
        int main(int argc, char **argv){
        	int n;
        	cin >> n;
        	f(n);
        	return 0;
        }
        
        • 0
          @ 2024-6-13 14:44:24
          #include<bits/stdc++.h>
          #define ll long long
          using namespace std;
          int n;
          void mix(char i,string w){
          	if(int(i-'0')>n) for(int j=0;j<w.size();j++) cout<<int(w[j]-'0')<<' ';
          	else{
          		string t=w;
          		w=t+i+t;
          		mix(i+1,w);
          	}
          }
          int main(){
          	system("color F1");
          	cin>>n;
          	mix('2',"1");
          	return 0;
          }
          
          • -3
            @ 2023-12-22 21:10:46
            #include<bits/stdc++.h>
            using namespace std;
            string dg(int n){
            	if(n==1)
            	return "1 ";
            	string s;
            	int N=n;
            	while(n>0){
            		s+=char(n%10+'0');
            		n/=10;
            	}
            	reverse(s.begin(),s.end());
            	return dg(N-1)+s+" "+dg(N-1);
            }
            int main(){
            	int n;
            	cin>>n;
            	cout<<dg(n);
            }
            
            • 1

            Information

            ID
            964
            Time
            1000ms
            Memory
            256MiB
            Difficulty
            6
            Tags
            (None)
            # Submissions
            46
            Accepted
            15
            Uploaded By