Java 递归问题-给定数组n和数字k
给定一个数组大小n和一个正数max(max表示可以用于放置在数组中的数字的范围) 我想计算我可以在数组中放置多少个已排序的数字组合 例如: 如果Java 递归问题-给定数组n和数字k,java,sorting,recursion,Java,Sorting,Recursion,给定一个数组大小n和一个正数max(max表示可以用于放置在数组中的数字的范围) 我想计算我可以在数组中放置多少个已排序的数字组合 例如: 如果n=3,max=2(我们只能使用1/2,因为max是2),那么有4种排序数组组合 1. {1,1,1} 2. {1,1,2} 3. {1,2,2} 4. {2,2,2} 我写了一些代码并成功地通过了这个特定的示例,但是任何其他max>2的示例都不会返回正确的答案 我发现的问题是,当递归到达最后一个索引时,它不尝试第三个数字,而是向后折叠 我的代
n=3,max=2(我们只能使用1/2,因为max是2),那么有4种排序数组组合
1. {1,1,1}
2. {1,1,2}
3. {1,2,2}
4. {2,2,2}
我写了一些代码并成功地通过了这个特定的示例,但是任何其他max>2
的示例都不会返回正确的答案
我发现的问题是,当递归到达最后一个索引时,它不尝试第三个数字,而是向后折叠
我的代码:
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if(numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if(index == n) {
return 1;
}
int sortTwo = howManySorted(n, max, index+1, numToMax, numToMax);
int sortOne = howManySorted(n, max, index+1, numToMax+1, numToMax);
return ((sortOne+sortTwo));
}
public static int howManySorted(int n, int max) {
return howManySorted(n, max, 0, 1, 0);
}
private static int howManySorted(int n,int max,int index,int numToMax,int prevNum){
//如果该值大于最大值,则返回0
如果(numToMax>最大值){
返回0;
}
如果(numToMax
我认为您需要更改两个递归调用(这就是为什么它只达到值2),并根据max
参数执行尽可能多的调用:
private static int howManySorted(int n, int max, int index, int numToMax, int prevNum) {
// If the value is bigger then max return 0
if (numToMax > max) {
return 0;
}
if (numToMax < prevNum) {
return 0;
}
//If Index reached the end of the array return 1
if (index == n) {
return 1;
}
int result = 0;
for (int i = 0; i < max; i++)
result += howManySorted(n, max, index + 1, numToMax + i, numToMax);
return result;
}
private static int howManySorted(int n,int max,int index,int numToMax,int prevNum){
//如果该值大于最大值,则返回0
如果(numToMax>最大值){
返回0;
}
如果(numToMax
我相信您可以简化对类似问题的回答
private static long howManySorted(int length, int min, int max) {
if (length == 1) {
return max - min + 1;
}
// if (min == max) {
// return 1;
// }
long result = 0;
for (int i = min; i <= max; i++) {
result += howManySorted(length - 1, i, max);
}
return result;
}
public static long howManySorted(int length, int max) {
if ((length < 1) || (max < 1)) {
throw new IllegalArgumentException();
}
return howManySorted(length, 1, max);
}
private static long howManySorted(int-length、int-min、int-max){
如果(长度==1){
返回最大值-最小值+1;
}
//如果(最小值==最大值){
//返回1;
// }
长结果=0;
对于(int i=min;i只需测试我的代码,我想它就能解决您的问题:
class Test {
private static int howManySorted(int n, int max) {
//Better time complexity if u use dynamic programming rather than recursion.
if (n == 0) return 1;
int res = 0; // "res" can be a very large.
for (int i = n; i >= 1; i--) {
for (int j = max; j >= 1;j--) {
res += howManySorted(i-1, j-1);
}
}
return res;
}
public static void main(String[] args) {
System.out.println(howManySorted(3, 2));
}
}
如果你使用动态规划,并且注意答案,这段代码会运行得更快,它可能是一个非常大的整数。你们忘了他需要一个只使用递归的解决方案。
可能是CS类的Java赋值
我也有这个问题
这就是我的答案:
/**
* @param n Number of values in the array
* @param max Maximum value of each cell in the array
* @return int
*/
public static int howManySorted(int n, int max) {
return howManySorted(max, max, 1, n - 1);
}
/**
*
* @param value The current value
* @param max The maximum possible value (not allowed to use global parameters, so the parameter value always stays the same)
* @param min The minimum value allowed in this index. Determined by the value of the previous index (when first called, use value 1)
* @param index The index of the value being manipulated
* @return
*/
public static int howManySorted(int value, int max, int min, int index) {
//If any of these cases are found true, it means this value is invalid, don't count it
if (index < 0 || value < min) {
return 0;
}
//First lower the value in the same index, the result is the number of valid values from that branch
int lowerValue = howManySorted(value - 1, max, min, index);
//Now check all the valid values from the next index - value is max-1 to prevent counting twice some numbers
int lowerIndex = howManySorted(max - 1, max, value, index - 1);
//Return 1 (this number we are at right now) + all of its children
return 1 + lowerValue + lowerIndex;
}
/**
*@param n数组中的值数
*@param数组中每个单元格的最大值
*@return int
*/
公共静态int howManySorted(int n,int max){
返回howManySorted(max,max,1,n-1);
}
/**
*
*@param value当前值
*@param max最大可能值(不允许使用全局参数,因此参数值始终保持不变)
*@param min此索引中允许的最小值。由上一个索引的值确定(首次调用时,使用值1)
*@param index正在操作的值的索引
*@返回
*/
公共静态int howManySorted(int值、int最大值、int最小值、int索引){
//如果这些情况中的任何一个为真,则表示该值无效,不要计算它
如果(指数<0 | |值
从“{1”开始,在每个递归中添加元素“{1,1”和/或值“{2”。当它到达n个元素数组时,我们将其添加到计数器中。n是数组中的元素数max是每个元素的最大值。min是1。element是正在操作的数组中的当前单元格。我们从1开始(在实际数组中表示0).value是当前元素的当前值。我们从1开始
// external function according to the given question
public static int count (int n, int max)
{
return count(n,max, 1, 1);
}
private static int count (int n, int max, int element, int value)
{
int counter = 0;
// only if our array reached n elements we count the comination
if (element == n)
counter++;
else // we need to continue to the next element with the same value
counter += count(n, max, element +1, value);
if (value < max) // if our current element didn't reach max value
counter += count (n, max, element, value+1);
return counter;
}
//根据给定问题的外部函数
公共静态整数计数(整数n,整数最大值)
{
返回计数(n,最大值,1,1);
}
私有静态int计数(int n,int max,int元素,int值)
{
int计数器=0;
//只有当我们的数组达到n个元素时,我们才能计算联合
if(元素==n)
计数器++;
else//我们需要继续到具有相同值的下一个元素
计数器+=计数(n,最大值,元素+1,值);
if(value
我将每个系列(例如“1,1,2”)视为一个数组,因此在开始时我编写了如下内容:
public static void main(String[] args)
{
System.out.println(howManySorted(3, 2, 1, "")); // 4
System.out.println(howManySorted(2, 3, 1, "")); // 6
}
private static int howManySorted(int n, int max, int index, String builder)
{
if (max == 0) // if we exceeds max, return 0.
return 0;
if (n == 0) // num represents how many individual numbers we can have, if it is zero it means we have the required length (e.g. if n = 3, we have 'x1,x2,x3').
{
System.out.println(builder.substring(0, builder.length() - 2));
return 1;
}
int r1 = howManySorted(n - 1, max, index, builder + index + ", "); // i added additional var 'index' to represent each number in the list: (1,1,1)
int r2 = howManySorted(n, max - 1, index + 1, builder); // I'm increasing the index and decreasing the max (1,1,**2**)
return r1 + r2;
}
但最终,我们不需要“索引”或“建设者”,他们只是强调我是如何解决它的
public static void main(String[] args)
{
int max = 2, n = 3;
System.out.println(howManySorted(n, max)); // 4
int max1 = 3, n1 = 2;
System.out.println(howManySorted(n1, max1)); // 6
}
public static int howManySorted(int n, int max)
{
if (max == 0) // if we exceeds max, return 0.
return 0;
if (n == 0) // like we said, num represents how many individual numbers we can have, if it is zero it means we have the required length (e.g. if n = 3, we have 'x1,x2,x3').
return 1;
int r1 = howManySorted(n - 1, max);
int r2 = howManySorted(n, max - 1);
return r1 + r2;
}
好奇:当nummax
为真时,你只能在代码中递归两次,即下一个数字只能与上一个数字相同或更高,这意味着对于max>2
你将找不到像1,1,5
这样的解决方案,因为这是4的跳过。是的,我明白这就是我在这里发布的原因(:FYI:Parameterindex
是多余的。在递归调用中,不是将index
从0
向上计数到n
,而是将n
向下计数到0
。因此,在递归调用周围添加一个循环,从numToMax
循环到max
——同样,在给出我的第一个注释时,ParameterprevNum
是多余的。请删除它。请添加一个