赞
踩
中秋节,公司分月饼,m个员工,买了n个月饼,m<=n,每个员工至少分1个月饼,但可以分多个,单人份到最多月饼的个数为Max1,单人分到第二多月饼的个数是Max2,Max1-Max2<=3,。同理,单人分到第n-1多月饼的个数是Max(n-1),单人分到第n多月饼的个数是Max(n),Max(n-1)-Max(n)<=3。请问有多少种分月饼的方法?
#include <stdio.h> #include <vector> std::vector<int> vec; int dispatch(int nPeople, int nMoon, int nMin) { if (nMoon <= 0) { return 0; // 分配失败 } if (nPeople <= 0) { return 0; } if (nPeople == 1) { if (nMoon >= nMin && nMoon <= nMin + 3) { vec.push_back(nMoon); for (std::vector<int>::iterator x = vec.begin(); x != vec.end(); x ++) { printf("%d ", *x+1); } printf("\n"); vec.pop_back(); return 1; } return 0; } int nCount = 0; for (int got = nMin; got <= nMin+3; got++) { vec.push_back(got); nCount += dispatch(nPeople-1, nMoon-got, got); vec.pop_back(); } return nCount; } int main(void) { int nPeople = 10, nMoon = 34; nMoon -= nPeople; int nCount = 0; // 先确定分得月饼最少的那个人得到的月饼数 for (int got = 0; got <= nMoon; got++) // got表示分到月饼最少的那个人得到的月饼数 { vec.push_back(got); nCount += dispatch(nPeople-1, nMoon-got, got); // 第二少的人只能分[got,got+3]个 vec.pop_back(); } printf("%d\n", nCount); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。