zoukankan      html  css  js  c++  java
  • 蓝桥杯——说好的进阶之和式分解

    对于正整数 n,输出和等于 n且组成和式的数字从左至右是非递增的全部正整数和式。

    输入:

    6

    输出:

    6=6
    6=5+1
    6=4+2
    6=4+1+1
    6=3+3
    6=3+2+1
    6=3+1+1+1
    6=2+2+2
    6=2+2+1+1
    6=2+1+1+1+1
    6=1+1+1+1+1+1

    import java.util.Scanner;
    
    public class Main{
    
    	static int[] a = new int[1000];
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner scanner=new Scanner(System.in);
    		int n=Integer.parseInt(scanner.nextLine());
    		a[0] = n;
    		cal(n, 1);
    	}
    
    	static void cal(int n, int p) {
    		for (int i = n < a[p - 1] ? n : a[p - 1]; i > 0; i--) {
    			a[p] = i;
    			if (n == i) {
    				System.out.printf("%d=%d", a[0], a[1]);
    				for (int j = 2; j <= p; j++) {
    					System.out.printf("+%d", a[j]);
    				}
    				System.out.println();
    			} else {
    				cal(n - i, p + 1);
    			}
    		}
    	}
    }
    


  • 相关阅读:
    1. flask框架-简介/快速启动
    conda创建、查看、删除虚拟环境
    DOM用法(二)
    DOM用法(一)
    BOM用法
    Javascript对象
    JavaScript基础篇
    mysql 外连接
    mysql 连接表 内连接 inner
    mysql分组函数
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3768152.html
Copyright © 2011-2022 走看看