Re: [理工] [軟設]-中正96-資工
※ 引述《alibudaken (最後掙扎J)》之銘言:
: 12.
: (a) Given an array A of N positive integers and an integer M
: write a recursive C function
: int sos(int A[], int N, int M)
: which returns 1 if there exists a subset of numbers in A
: whose sum is equal to M and returns 0 otherwise.
: Your function should not exceed 15 statements.
: 是用類似Dynamic programming的概念去找嗎?
: 想了想還是想不出來...OTL
: 請幫忙解惑<(_ _)>
: 謝謝!!
int sos(int A[], int N, int M)
{
if( N==1 ){
return (A[0]==M);
}
else{
return ( sos(A+1, N-1, M-A[0]) || sos(A+1, N-1, M) );
}
}
大概是這樣 :)
--
人家可不是為了你才這樣做的哦!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.198.35.85
推
03/23 23:58, , 1F
03/23 23:58, 1F
A[0]就是Array的第一個element
※ 編輯: dendrobium 來自: 60.198.35.85 (03/24 07:58)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):