半数序列集(递归no.4)

来源:JK老班

题目:给定数字n,n的半数序列集是(1)在 n 的右边加上一个自然数,但该自然数不能超过最近添加的数的一半,这样生了新的序列;(2)按此规则进行处理,直到不能再添加自然数为止。例如,4的半数序列集是{4,4 2,4 2 1,4 1}。

输入:一个整数 n,(0<n<=50)。

输出:按照数字降序,输出集合所有序列,每个序列一行,每个数字后面跟一个空格。

样例输入

6

样例输出

6 3 1 
6 3 
6 2 1 
6 2 
6 1 
6 

import java.util.Scanner;
//1470:半数序列集
public class P1470 {

    int n;
    int[] A;
    public P1470() {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        A=new int[n]; A[0]=n;
        Search(1);
    }
    
    void Search(int pos) {
        for(int i=A[pos-1]/2;i>=1;i--) {//加数字
            A[pos]=i;
            Search(pos+1);
        }
        
        //不加数字,直接输出
        for(int k=0;k<pos;k++) System.out.print(A[k]+" ");
        System.out.println();
    }
    
    public static void main(String[] args) {
        P1470 p=new P1470();

    }

}
 

猜你喜欢

转载自blog.csdn.net/llllll_____/article/details/88869100