Re: [討論] 面試遇到的考題

看板Soft_Job作者 ( )時間11年前 (2014/07/03 17:29), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串6/27 (看更多)
※ 引述《sleeper0121 (sleeper)》之銘言: : 今天去面試,裡面有題題目是這樣: : 寫個函式,傳個整數陣列進去,陣列裡面的整數可以是正數、負數或 0 : 請回傳一個陣列裡面相鄰互乘的最大整數值 : 例如: [2 , -7 , 0 , 2 , 3 , 8 , -6 , 5] : 就是 2 * 3 * 8 = 48 : 再一個例子: [-2 , 0 , 3 , 5 , -7] : 就是 3 * 5 = 15 : 請問這題思考邏輯大概是怎樣呢? : 當下沒解出來,害我回家後還一直再想 XD 剛剛隨便寫的... #include <stdio.h> int maxSubListProduct(int* list, int n) { int max = list[0]; int curMax = list[0]; int curMin = list[0]; int pMin; int pMax; int tmp; int i; for(i=1; i<n; i++) { pMin = list[i] * curMin; pMax = list[i] * curMax; if(pMin > pMax) { tmp = pMin; pMin = pMax; pMax = tmp; } if(pMin > list[i]) curMin = list[i]; else curMin = pMin; if(pMax < list[i]) curMax = list[i]; else curMax = pMax; if(curMax > max) max = curMax; } return max; } int main() { int list[] = {2, -7, 0, 2, 3, 8, -6, 5, -1}; int answer = maxSubListProduct(list, sizeof(list)/sizeof(int)); printf("answer = %d\n", answer); return 0; } 希望是正確的 XD -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.21.158.103 ※ 文章網址: http://www.ptt.cc/bbs/Soft_Job/M.1404379798.A.4F4.html
文章代碼(AID): #1JjIAMJq (Soft_Job)
討論串 (同標題文章)
文章代碼(AID): #1JjIAMJq (Soft_Job)