当前位置:网站首页>The recursive recursive Fighting_ silver study ah but level 4

The recursive recursive Fighting_ silver study ah but level 4

2022-08-10 13:21:00 51CTO


Approximate title:


There are n seats in a row, and at least two seats are vacant between every two people, and the practice in the case of at least one person is required.


Thinking:


a[n] is all practices that have n seats.Assuming that the previous actions have been arranged, there are two situations for the nth seat, someone does it and no one does it. When someone does it, n-1 and n-2 must be done by no one, so the sitting method and a[n-3] is the same, when no one is sitting, it is the same as sitting on a[n-1], but there is another case, that is, when there is only one person sitting, on a[n]Sitting by one person is also considered a situation, so the formula a[n]=a[n-1]+a[n-3]+1 is obtained.


Thinking:


This question is a bit difficult to think about, but it is not difficult to think that all the sitting methods can be deduced by formula, and generally the latter data is related to the former, but it is easy to add one at the endForget it, but when debugging, you will find that each number will be smaller by 1, and if you think about it carefully, you will find the error.


Code:

#include

using namespace std ;

int main()
{ int n ;
int a [ 46 ];
a [ 1 ]= 1 ;a [ 2 ]= 2 ;a [ 3 ]= 3 ;
for ( int i = 4 ;i < 46 ;i++)
{a [i ]=a [i -1 ]+a [i -3 ]+ 1 ;

}
while (cin >>n )
{cout <

}
return 0 ;
}
原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101233203298.html