codevs 2287 火车站

2287 火车站

 

 时间限制: 1 s
 空间限制: 32000 KB
 题目等级 : 钻石 Diamond
 
 
题目描述 Description

火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上、下车,但上、下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人。从第3站起(包括第3站)上、下车的人数有一定的规律:上车的人数都是前两站上车人数之和,而下车人数等于上一站上车人数,一直到终点站的前一站(第n-1站),都满足此规律。现给出的条件是:共有N个车站,始发站上车的人数为a,最后一站下车的人数是m(全部下车)。试问从x站开出时车上的人数是多少?若无解输出“No answer.”(所有数据均在longint范围内)

输入描述 Input Description

a,n,m和x

输出描述 Output Description

x站开出时车上的人数

样例输入 Sample Input

1 6 7 3

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

分类标签 Tags 
递推 数论
 
/*
好几个月之前做的一道题了,枚举+递推,超级恶心 
*/
#include<iostream>
#include<cstdio>
using namespace std;
int a,n,m,x;
bool b;
struct node{int up,tot;}f[501];
int main()
{
    cin>>a>>n>>m>>x;
    f[1].up=a;f[1].tot=a;
    f[2].tot=a;f[n].tot=m;
    f[n].up=0;
    for(int k=0;k<=m;k++)
    {
        f[2].up=k;
        for(int i=3;i<n;i++)
        {
            f[i].up=f[i-1].up+f[i-2].up;
            f[i].tot=f[i-1].tot+f[i].up-f[i-1].up;
        }
        if(f[n-1].tot==m)
        {
            printf("%d",f[x].tot);
            b=true;
            return 0;
        }
    }
    if(!b)printf("No answer.\n");
    return 0;
}


//第一次做的时候的代码:
#include<iostream>
#include<cstdio>
using namespace std;
int a,n,m,x;
bool b;
struct car
{
    int up;
    int down;
    int tot;
}f[501];
int main()
{
    cin>>a>>n>>m>>x;
    f[1].up=a;
    f[1].tot=a;
    f[2].tot=a;
    f[n].tot=m;
    f[n].up=0;
    for(int k=0;k<=m;k++)
    {
        f[2].up=k;
        for(int i=3;i<n;i++)
        {
            f[i].up=f[i-1].up+f[i-2].up;//A
            //f[i].down=f[i-1].up;B
            //f[i].tot=f[i-1].tot+f[i].up-f[i].down;C
            f[i].tot=f[i-1].tot+f[i].up-f[i-1].up;//D<--C(B)
        }
        if(f[n-1].tot==m)
        {
            cout<<f[x].tot;
            b=true;
            return 0;
        }
    }
    if(!b)
        cout<<"No answer.";
    return 0;
}

 

转载于:https://www.cnblogs.com/EvilEC/p/6076240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值