一、思路分析
1、题目链接
链接: P1011 [NOIP1998 提高组] 车站
2、分析
(1)本题其实就是模拟,或者说找规律
(2)我们列举几个就能看出规律了
因为我们不知道第二个车站上车人数和下车人数,我们就假设上下车人数为a+t(第二站上下车人数要相同)
车站 1 2 3 4 5 6 7
上车 a a+t 2a+t 3a+2t 5a+3t 8a+5t 13a+8t
下车 0 a+t a+t 2a+t 3a+2t 5a+3t 8a+5t
剩余 a a 2a 3a+t 5a+2t 8a+4t 13a+7t
(3)我们只看上车人数,每次上车人数都是前两次上车人数之和,不难看出斐波那契函数
int re[25] = {0};
re[1] = 1;
for(i = 2; i <= n; i++)
re[i] = re[i - 1] + re[i - 2];
(4)而对于车上剩余人数,其实和上车人数就相差t,所以我们主要就是求t以及上车人数
temp = (m - re[n - 1] * a) / (re[n - 2] - 1);
cout<<re[x] * a + temp * (re[x - 1] - 1)<<endl;
(5)有些特殊情况,第一站、第二站和最后一站
if(x == n)
cout<<0<<endl;
else if(x == 1 || x == 2)
cout<<a<<endl;
二、整体代码
#include<iostream>
using namespace std;
int main() {
int a, n, m, x, i, temp;
cin>>a>>n>>m>>x;
if(x == n)
cout<<0<<endl;
else if(x == 1 || x == 2)
cout<<a<<endl;
else {
int re[25] = {0};
re[1] = 1;
for(i = 2; i <= n; i++)
re[i] = re[i - 1] + re[i - 2];
temp = (m - re[n - 1] * a) / (re[n - 2] - 1);
cout<<re[x] * a + temp * (re[x - 1] - 1)<<endl;
}
return 0;
}