偶数子序列


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
let allarr;
let alltemp,restemp=[];
let nn,kk;
let res;
function begin(n){
alltemp=new Array(n).fill(0)
}
// function clear(){
// alltemp==new Array(nn).fill(0)
// restemp=[]
// }

function fn(x,s){
// console.log("restemp:"+restemp);
// console.log("x:"+x);
if(kk==x){
// console.log("kk x restemp:"+restemp);
// console.log("kk:"+kk);
// console.log("x:"+x);
if(s%2==0)res++;
return ;
}
for (let i = 0; i < nn; i++) {
if(!alltemp[i]){
restemp.push(allarr[i])
alltemp[i]=1
s+=allarr[i]
// let y=x+1
fn(x+1,s)
alltemp[i]=0
restemp.pop()
}
}
}

function main(n,k,arr){
res=0
allarr=arr;
nn=n;
kk=k;
begin(n);
fn(0,0)
return res/jiecheng(k);
}

function jiecheng(x){
if(x==1)return 1;
return x*jiecheng(x-1)
}
console.log(main(5,2,[1,2,3,4,5]));
console.log(main(3,1,[1,2,3]));
console.log(main(4,3,[1,4,2,2]));


Author: pkq
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source pkq !
  TOC