先占个位置,详细思路之后再写;
先贴个图片和源码上去:
#include <iostream> using namespace std; int first_day_food(int day, int food) { if (day <= 0 || day > food) return 0; else { int times = 0, remain = food; if (day < 31) // 如果天数大于30天,那么食物绝对不会多(否则食物数量会超出int的上限) times = food / ((1<<day)-1), remain = food % ((1<<day)-1); if (times > 0) // 食物太多了: return times*(1<<(day-1)) + first_day_food(day, remain); else { int d = 0; // 食物不够多: // 找出本趟可以吃的最大数量:2^d + 2^(d-1) + ... + 2 + 1 + ( ... + 1) for (; (1<<d)-1+day-d <= food; d++); d--; // 递归计算下一趟分配的食物,并将第一天累加: return (1<<(d-1)) + first_day_food(d, food-((1<<d)-1+day-d) ); } } } int main() { int day, food; cin >> day >> food; cout << first_day_food(day, food); return 0; }