简体   繁体   English

返回数字分区的功能

[英]Function to return number partitions

I was looking for a function that returns the partitions of an integer. 我正在寻找一个返回整数分区的函数。 I found almost what I wanted in this question: 在这个问题中,我几乎找到了我想要的:

Print all unique integer partitions given an integer as input 给定整数作为输入,打印所有唯一整数分区

This function prints the partitions of an integer. 此函数显示整数的分区。 But how can I change it in order to actually store and return all the partitions (maybe in a List)? 但是,如何更改它以实际存储和返回所有分区(也许在列表中)?

I'm aware that Java has the combinatoricslib . 我知道Java具有combinatoricslib Is there something similar in C#? C#中是否有类似的东西? If not, how could I make that function return all possible partitions? 如果没有,如何使该函数返回所有可能的分区?

The link you gave are simply printing the result, but you want the result returned. 您提供的链接只是打印结果,但是您希望返回结果。 The best way is likely to add an extra "result collector" parameter the "print" method, and have that method add to the collector instead of printing each partition. 最好的方法可能是在“打印”方法中添加一个额外的“结果收集器”参数,然后将该方法添加到收集器中,而不是打印每个分区。

Depending on what you want, your collector would be one of the following: 根据您的需求,您的收集器将是以下之一:

// Each partition is a string of the numbers, like when printed
List<String> partitions;

// Each partition is a list of the numbers
List<List<Integer>> partitions;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM