1 solutions

  • 0
    @ 2024-12-11 0:16:56

    C++ :

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int a[110];
    int n,m;
    void so(int k){
    	for(int i=a[k-1]+1;i<=n-(m-k);++i){
    		a[k]=i;
    		if (k==m){
    			for(int j=1;j<=m;++j) printf("%3d",a[j]);
    			printf("\n");
    		}
    		else so(k+1);
    	}
    }
    int main(){
        cin>>n>>m;
        if (m==0) return 0;
    	so(1);	
    	return 0;
    }
    

    Java :

    import java.util.Scanner;
    
    
    public class Main {
    	static int[] a;
    	static boolean[] vis;
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		while(in.hasNextInt()){
    			int m = in.nextInt();
    			int n = in.nextInt();
    			vis = new boolean[m];
    			a = new int[m];
    			for(int i=0;i<m;i++)
    				a[m-i-1] = i+1;
    			com(m,n);
    		}
    	}
    
    	public static void com(int x,int y){
    		if(x<y)
    			return;
    		if(y==0){
    			print();
    			return;
    		}
    		vis[x-1] = true;
    		com(x-1,y-1);
    		vis[x-1] = false;
    		com(x-1,y);
    	}
    
    	public static void print() {
    		for (int i = a.length - 1; i >= 0; i--)
    			if (vis[i])
    				System.out.printf("%3d", a[i]);
    		System.out.println();
    	}
    }
    
    
    • 1

    Information

    ID
    692
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By