P1011 车站

题目链接:

https://www.luogu.org/problemnew/show/P1011

题意理解

显然,我们看到数据量那么小,可以来个模拟。直接暴力枚举一波即可。

我的代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static final int maxn = 30;
    static int a, n, m, x;
    static int[] nums = new int[maxn];
    static int[] getOn = new int[maxn];
    static int[] getOff = new int[maxn];
    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        a = fs.nextInt();
        n = fs.nextInt();
        m = fs.nextInt();
        x = fs.nextInt();
        for(int i = 0; i <= 20; i++) {
            getOn[1] = a;
            getOff[1] = 0;
            nums[1] = a;
            getOn[2] = i;
            getOff[2] = i;
            nums[2] = a;
            for(int j = 3; j <= n - 1; j++) {
                getOn[j] = getOn[j-1] + getOn[j-2];
                getOff[j] = getOn[j-1];
                nums[j] = nums[j-1] + getOn[j] - getOff[j];
            }
            if(nums[n - 1] == m) {
                System.out.println(nums[x]);
                return;
            }
        }
    }

    public static class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        public void eat(String s) {
            st = new StringTokenizer(s);
        }

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
            eat("");
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (Exception e) {
                // TODO: handle exception
            }
            return null;
        }

        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s = nextLine();
                if(s == null) {
                    return false;
                }
                eat(s);
            }
            return true;
        }

        public String nextToken() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.valueOf(nextToken());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值