请任何人都可以帮我解决这个问题:使用迭代方法 T (n) = T (n - 1) + (n - 1) 解决
并证明 T (n) ?? (n²)
拜托,如果你能一步一步地解释,我将不胜感激。
我目前正在尝试编写一些代码,在给定硬币值列表的情况下,它将返回总和为某个值的所有可能的硬币组合。这是程序应该如何运行的示例:
>>> find_changes(4,[1,2,3])
[[1, 1, 1, 1], [2, 1, 1], [1, 2, 1], [3, 1], [1, 1, 2], [2, 2], [1, 3]]
Run Code Online (Sandbox Code Playgroud)
我得到了以下代码模板来填写:
def find_changes(n, coins):
if n < 0:
return []
if n == 0:
return [[]]
all_changes = []
for last_used_coin in coins:
### DELETE THE "pass" LINE AND WRITE YOUR CODE HERE
pass
return all_changes
Run Code Online (Sandbox Code Playgroud)
我尝试在for循环中使用以下代码:
all_changes.append[last_used_coin]
find_changes(n-last_used_coin,coins)
Run Code Online (Sandbox Code Playgroud)
它目前不起作用。我究竟做错了什么?
我想定义一个递归函数,可以对任何整数列表进行排序:
def sort_l(l):
if l==[]:
return []
else:
if len(l)==1:
return [l[-1]]
elif l[0]<l[1]:
return [l[0]]+sort_l(l[1:])
else:
return sort_l(l[1:])+[l[0]]
Run Code Online (Sandbox Code Playgroud)
在列表 [3, 1, 2,4,7,5,6,9,8] 上调用这个函数应该给我:
[1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
但我得到:
print(sort_l([3, 1, 2,4,7,5,6,9,8]))--> [1, 2, 4, 5, 6, 8, 9, 7, 3]
Run Code Online (Sandbox Code Playgroud)
请帮我解决问题,实际代码将不胜感激。谢谢!
递归调用将在运行时堆栈上构建,然后在运行时堆栈"展开"时以相反的顺序计算值.第18行是我得到错误的地方,但我对于出了什么问题一无所知.编译完成.未编译以下文件:找到1个错误:[line:18]} else {错误:'else'没有'if'
public class Recursion {
public static void main(String[] args) {
int n = 7;
//Test out the factorial
System.out.println(n + " factorial equals ");
System.out.println(Recursion.factorial(n));
System.out.println();
}
public static int factorial(int n) {
int temp;
System.out.println("Method call -- calculating Factorial of: " + n);
{
int temp;
if (n == 0) {
return 1;
}
} else {
temp = factorial(n - 1);
System.out.println("Factorial of: " + (n - 1) + " is " + temp);
return …Run Code Online (Sandbox Code Playgroud) import java.util.Scanner;
public class factorial {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int x = 0, y;
System.out.println("enter factorial number");
if(in.hasNextInt()){
x = in.nextInt();// check if the scanner's next token is an int
}//end if
y = factorial(x);
System.out.println("factorials is "+y);
}//end main
private static int factorial(int n) {
// TODO Auto-generated method stub
if(n==0||n==1){
return 1;
}
else
return n*factorial(n-1);//recursive call
}//end factorial
}//end class
Run Code Online (Sandbox Code Playgroud) 我正在编写一个涉及链表的程序.我编写了一个函数,它返回链表中的第n个节点,它以递归方式调用自身.我的程序编译并运行直到递归函数然后崩溃.这是节点的构造函数以及递归函数:
LinkedList::LinkedList():
head(head){
sizeInt = 0;
}
Node* LinkedList::get_nth(const int& n) const {
Node* node = new Node();
for(int counter = 1; counter <= n; counter++){
node = get_nth(counter + 1);
}
return node;
}
Run Code Online (Sandbox Code Playgroud)
这个功能有什么问题?如果您需要更多详细信息或代码,请告诉我们.
我实现了一个递归函数计算x的y
public static int power(int x, int y){
if(y>0){
x = x*x;
power(x,y-1);
}
return x;
}
Run Code Online (Sandbox Code Playgroud)
该方法应该返回x ^ y的值,但它返回原始x值的平方(x ^ 2),即使y大于2,我缺少什么?
我正在寻找最快的方式来递归扫描所有现有文件和文件夹的目录。
例:
- images
-- image1.png
-- image2.png
-- thumbnails
--- thumb1.png
--- thumb2.png
- documents
-- test.pdf
Run Code Online (Sandbox Code Playgroud)
应该返回:
因此,我将从以下内容开始:
$filesandfolders = @scandir( $path );
foreach ($filesandfolders as $f){
if(is_dir($f)){
//this is a folder
} else {
//this is a file
}
}
Run Code Online (Sandbox Code Playgroud)
但这是最快的方法吗?
为什么这段代码有效?
public static String reverse(String a) {
if(a.length() == 0) {
return a;
} else {
return reverse(a.substring(1)) + a.charAt(0);
}
}
Run Code Online (Sandbox Code Playgroud)
这不是吗?:
public static String reverse(String a) {
if(a.length() == 0) {
return a;
} else {
return reverse(a.substring(1)) + a.substring(0);
}
}
Run Code Online (Sandbox Code Playgroud)
此外,递归如何在案例1中起作用,添加有a.charAt(0)什么作用?这种方法如何达到基本情况?
考虑一个函数,该函数执行一个操作并且无论输入是什么,最多只能调用一次(不超过一个“递归”级别)。该函数是否被认为是递归的?
例:
join1 :: [Maybe a] -> [a]
join1 [Just x] = [x]
join1 [Nothing] = []
join1 (x) = concat (map join1 (map (\k->[k]) x))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在调用时:Join1 [什么都没有,只有2,只有3,...]将在每个元素上调用join1函数,立即输入终止条件
recursion ×10
java ×4
python ×2
algorithm ×1
c++ ×1
combinations ×1
factorial ×1
haskell ×1
if-statement ×1
iteration ×1
linked-list ×1
list ×1
php ×1
recurrence ×1
scandir ×1
trace ×1