[問題] 空間複雜度?

看板C_and_CPP (C/C++)作者 (皮拉斯)時間14年前 (2012/02/29 09:54), 編輯推噓0(004)
留言4則, 3人參與, 最新討論串1/1
1. float sum(float list[], int n) { float tempsum = 0; int i; for( i = 0; i <n ; i++) tempsum += list[i]; return tempsum; } Ssum(I) = 0 C pass all parameters by value. When an array is passed as an argument to a function, C interprets it as passing the address of the first element of the array. (不懂這句是什麼意思?) 為什麼宣告float tempsum = 0 是fixed? 2. float rsum(float list[], int n) { if (n) return rsum(list,n-1)+list[n-1]; return 0; } Space needed for one recursive call: Type Name # of bytes parameter: array pointer list [] 4 parameter: integer n 4 return address:(used internally) 4 ------------------------------------------------------------- TOTAL per recursive call 12 這一個return要算4bytes前一個不用? (跟傳值傳址有關,不太清楚) 謝謝回答 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 58.114.205.226

02/29 12:33, , 1F
有點不懂在問什麼 但空間複雜度1. O(1) 2. O(n)
02/29 12:33, 1F

02/29 12:38, , 2F
在C傳一個陣列事實上他是傳陣列第一個元素的位址
02/29 12:38, 2F

02/29 13:48, , 3F
第一個不用算return我猜是因為他是constant space~
02/29 13:48, 3F

02/29 16:37, , 4F
因為 Space needed for one recursive call
02/29 16:37, 4F
文章代碼(AID): #1FJOLfnm (C_and_CPP)
文章代碼(AID): #1FJOLfnm (C_and_CPP)