gpt4 book ai didi

c - C语言中的 friend 对算法递归解决方案

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:52:00 25 4
gpt4 key购买 nike

<分区>

Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up. Examples: Input : n = 3 Output : 4

Explanation: [{1}, {2}, {3} : all single ] [{1}, {2,3}] : 2 and 3 paired but 1 is single] similarly [{1,2}, {3}]
[{1,3}, {2}]

finally answer 4

这里我坚持构造递归

int friends(int i)
{
if(i==0){
return 0;
}
if(i==1){
return 1;
}
friends(i)=friends(i-1)+(i-1)*friends(i-2);
}

进一步引用:http://www.geeksforgeeks.org/friends-pairing-problem/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com