5 条题解

  • 3
    @ 2023-5-2 19:05:39
    #include <stdio.h>
    int n,m;
    void dfs(int p,int sum,int state)
    {
        if(sum + n - p<m) return; 
        if(sum==m)
        {
            for(int i=0;i<n;i++)
                if(state>>i&1)
                    printf("%d ",i+1);
            printf("\n");
            return;
        }
        if(p==n) return;
        dfs(p+1,sum+1,state|1<<p);
        dfs(p+1,sum,state);
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        dfs(0,0,0);
    }
    
    • 1
      @ 2023-10-29 17:30:26

      #include <bits/stdc++.h> using namespace std;

      int n, m; int res[1234567];

      void print () { for (int i = 1; i <= m; i++) { cout << res[i] << " "; } cout << endl; }

      void dfs (int cur) { if (cur > m) { print (); return ; } for (int i = res[cur - 1] + 1; i <= n; i++) { res[cur] = i; dfs (cur + 1); } }

      int main() { cin >> n >> m; dfs (1); return 0; }

      纯分享

      • 1
        @ 2023-4-1 19:52:23
        #include <queue>
        #include <math.h>
        #include <stack>
        #include <stdio.h>
        #include <iostream>
        #include <vector>
        #include <iomanip>
        #include <string.h>
        #include <algorithm>
        using namespace std;
        #define LL long long
        const int N = 1e5 + 10;
        const int INF = 0x3f3f3f3f;
        int a[N];
        int n ,m;
        void f(int last , int step)
        {
        	if(step == m)
        	{
        		for(int i = 0 ; i < step ; i++)
        			cout << a[i] << " ";
        		cout << endl;
        		return ;
        	}
        	for(int i = last +1 ; i <= n ; i++)
        	{
        		a[step] = i;
        		f(i , step + 1);
        	}
        }
        int main()
        {
        	cin >> n >> m;
        	f(0 , 0);
        	return 0;
        }
        
        • 0
          @ 2024-6-23 11:30:20
          #include<bits/stdc++.h>
          using namespace std;
          int boxes[25],n,m,numbers[105];
          void dfs(int s){
          	if(s > m){
          		for(int i = 1;i <= m;i++){
          			cout << boxes[i] << " ";
          		}
          		cout << endl;
          		return;
          	}
          	for(int i = boxes[s - 1];i <= n;i++){
          		if(not numbers[i]){
          			numbers[i] = 1;
          			boxes[s] = i;
          			dfs(s + 1);
          			numbers[i] = 0;
          		}
          	}
          }
          int main(){
          	cin >> n >> m;
          	boxes[0] = 1;
          	dfs(1);
          
          }
          
          • 0
            #include <bits/stdc++.h>
            using namespace std;
            const int N=1e2+10;
            const int INF=0x3f3f3f3f;
            int n,m,a[N];
            void dfs(int x)
            {
            	if(x==n)
            	{
            		for(int i=1;i<=x;i++)
            		{
            			cout<<a[i];
            		}
            		cout<<endl;
            		return;
            	} 
            	for(int i=a[x]+1;i<=m;i++)
            	{
            		a[x+1]=i;
            		dfs(x+1);
            		a[x+1]=0;
            	}
            }
            int main()
            {
            	cin>>m>>n;
            	dfs(0);
            	return 0;
            }
            
            • 1

            信息

            ID
            6
            时间
            1000ms
            内存
            128MiB
            难度
            3
            标签
            递交数
            405
            已通过
            231
            上传者