64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
int mouse(int m){
return
m <= 0 ? 0
: m == 1 || m ==2 ? 1
: m==7 ? mouse(m-1)+mouse(m-2)- 2
: mouse(m-1)+mouse(m-2) - mouse(m-5);
}
int main(){
const int month = 10;
std::cout << month <<"月份存活数: "<<mouse(month);
}
#include <cassert>
#include <vector>
#include <iostream>
using namespace std;
int get_count(int n);
class pairs_state /* sigleton */
{
private:
pairs_state()
{
count_of_month[0] = 1; /* sum */
count_of_month[1] = 1;
count_of_month[2] = 0;
count_of_month[3] = 0;
count_of_month[4] = 0;
count_of_month[5] = 0;
count_of_month[6] = 0;
count_of_month[7] = 0; /* alive for 7 months, always 0 */
}
pairs_state & operator ++ ()
{
int count_of_birth = count_of_month[2];
count_of_birth += count_of_month[3];
count_of_birth += count_of_month[4];
count_of_month[0] = 0;
for (int i = 6; i > 1; --i) {
count_of_month[i] = count_of_month[i-1];
count_of_month[0] += count_of_month[i];
}
count_of_month[1] = count_of_birth;
count_of_month[0] += count_of_month[1];
}
int sum() const
{
return(count_of_month[0]);
}
friend int get_count(int);
private:
pairs_state(const pairs_state &);
pairs_state & operator = (const pairs_state &);
private:
int count_of_month[8];
};
int get_count(int n)
{
assert(n > 0);
static pairs_state state;
static vector<int> count;
for (int i = count.size(); i < n; ++i) {
count.push_back(state.sum());
++state;
}
return(count[n-1]);
}
int main(void)
{
for (int i = 10; i > 0; --i) {
cout << "month " << i << ", pairs: " << get_count(i) << endl;
}
return(0);
}
template<const int N>
int mouse()
{
return mouse<N-1>() + mouse<N-2>() - mouse<N-5>();
}
template<>
int mouse<0>(){
return 1;
}
template<>
int mouse<1>(){
return 1;
}
template<>
int mouse<2>(){
return mouse<1>()+1 ;
}
template<>
int mouse<3>(){
return mouse<2>()+1;
}
template<>
int mouse<4>(){
//除了昨天新生的baby不会生子,其它的都会在今天生。
//因为 昨天的baby = mouse<3>() - mouse<2>();所以今天总数为 mouse<3>() *2 - (mouse<3>() - mouse<2>() );
//化简为
return mouse<3>() +mouse<2>();
}
template<>
int mouse<5>(){
return mouse<4>() + mouse<3>() - 1; //一个绝育了。
}
template<>
int mouse<6>(){
return mouse<5>() + mouse<4>() - 1 - 1; //死掉一个;绝育一个。
}
#include <iostream>
int main(){
const int n = 18;
std::cout <<std::endl<< n << "月份存活数:"
<<mouse<n-1>() ;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int New[10] = {0,0,0,0,0,0,0,0,0,0};
unsigned int Sum[2] = {0,0};
int i;
New[0] = 1;
Sum[0] = 1;
printf("%d \n\r",1);
for(i = 1;i <20;i++)
{
New[(i&7)] = New[((i-2)&7)] + New[(i-3)&7] + New[((i-4)&7)];
Sum[(i&1)] = Sum[(i-1)&1] + New[(i&7)] - New[((i-6)&7)];
printf("%d \n\r",Sum[(i&1)]);
}
return 0;
}