题目链接:http://134.208.3.122/JudgeOnline/problem.php?id=1172
题目描述
公共汽车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达 第2站,在第2站有人上、下车,但上、下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人。从第3站起(包括第3站)上、下 车的人数有一定的规律:上车的人数都是前两站上车人数之和,而下车人数等于上一站上车人数,一直到终点站的前一站(第n-1站),都满足此规律。现给出的 条件是:共有n个车站,始发站上车的人数为a,最后一站下车的人数是m(全部下车)。试问从x站开出时车上的人数是多少?
输入
只有一行,四个整数a,n,m和x
输出
x站开出时车上的人数
样例输入
5 7 32 4
样例输出
13
代码
#include <iostream>
using namespace std;
int shA(int n) {
if (n == 1)
return 1;
else if (n == 2)
return 0;
else
return shA(n-1) + shA(n-2);
}
int shB(int n) {
if (n == 1)
return 0;
else if (n == 2)
return 1;
else
return shB(n-1) + shB(n-2);
}
int xiaA(int n) {
if (n==1 || n==2)
return 0;
else
return shA(n-1);
}
int xiaB(int n) {
if (n==1)
return 0;
else if (n == 2)
return 1;
else
return shB(n-1);
}
int lastA(int n) {
if (n==1 || n==2)
return 1;
else
return (shA(n) - xiaA(n)) + lastA(n-1);
}
int lastB(int n) {
if (n==1 || n==2)
return 0;
else
return (shB(n) - xiaB(n)) + lastB(n-1);
}
int main() {
int a, b, m, n, x, num;
cin>>a>>n>>m>>x;
b = (m-(lastA(n-1)*a)) / lastB(n-1);
num = (lastA(x)*a) + (lastB(x)*b);
cout<<num;
return 0;
}
2727

被折叠的 条评论
为什么被折叠?



