Square into Squares. Protect trees!(平方数分解平方和)

题目Square into Squares. Protect trees!(平方数分解平方和) from codewars https://www.codewars.com/kata/54eb33e5bc1a25440d000891/train

My little sister came back home from school with the following task: given a squared sheet of paper she has to cut it in pieces which, when assembled, give squares the sides of which form an increasing sequence of numbers. At the beginning it was lot of fun but little by little we were tired of seeing the pile of torn paper. So we decided to write a program that could help us and protects trees.

Task

Given a positive integral number n, return a strictly increasing sequence (list/array/string depending on the language) of numbers, so that the sum of the squares is equal to n².

If there are multiple solutions (and there will be), return as far as possible the result with the largest possible values:

Examples

decompose(11) must return [1,2,4,10]. Note that there are actually two ways to decompose 11², 11² = 121 = 1 + 4 + 16 + 100 = 1² + 2² + 4² + 10² but don't return [2,6,9], since 9 is smaller than 10.

For decompose(50) don't return [1, 1, 4, 9, 49] but [1, 3, 5, 8, 49] since [1, 1, 4, 9, 49] doesn't form a strictly increasing sequence.

Note

Neither [n] nor [1,1,1,…,1] are valid solutions. If no valid solution exists, return nilnullNothingNone (depending on the language) or "[]" (C) ,{} (C++), [] (Swift, Go).

The function "decompose" will take a positive integer n and return the decomposition of N = n² as:

  • [x1 ... xk] or
  • "x1 ... xk" or
  • Just [x1 ... xk] or
  • Some [x1 ... xk] or
  • {x1 ... xk} or
  • "[x1,x2, ... ,xk]"

depending on the language (see "Sample tests")

Note for Bash

decompose 50 returns "1,3,5,8,49"
decompose 4  returns "Nothing"

Hint

Very often xk will be n-1.

思路暴力求解

Code:(可以改写成c++)

import math
import numpy as np
def decompose(n):
    res = np.zeros(100, dtype=int)
    fun(n*n, res, 1, 0)
    rel = [i for i in res if i > 0]
    if len(rel) == 0:
        return None
    return sorted(rel)
def fun(n, res, jd, count):
    if n == 0:
        return True
    j =int((math.sqrt(n))) if jd != 1 else int(math.sqrt(n))-1 #处理第一次传入的参数
    for i in range(j, j//2, -1): #循环求解,剪枝:每次不用一直遍历到1,只要遍历前半部分就可以
        if not i in res:
            num = n - i**2
            res[count] = i
            if fun(num, res, 0, count+1):
                return True
    res[count] = 0 #
print(decompose(50))

short Code

def decompose(n):
    def _recurse(s, i):
        if s < 0:
            return None
        if s == 0:
            return []
        for j in range(i-1, 0, -1):
            sub = _recurse(s - j**2, j)
            if sub != None:
                return sub + [j]
    return _recurse(n**2, n)
原文地址:https://www.cnblogs.com/shish/p/12288695.html