码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA语法——递推数列

时间:2018-12-12 00:11:09      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:for   question   tle   term   www.   scan   can   nbsp   static   

题目描述

给定a0,a1,以及an=p*a(n-1) + q*a(n-2)中的p,q。这里n >= 2。 求第k个数对10000的模。

输入描述:

输入包括5个整数:a0、a1、p、q、k。

输出描述:

第k个数a(k)对10000的模。
链接:https://www.nowcoder.com/questionTerminal/d0e751eac618463bb6ac447369e4aa25
来源:牛客网

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        while (reader.hasNext()) {
            int a0 = reader.nextInt();
            int a1 = reader.nextInt();
            int p = reader.nextInt();
            int q = reader.nextInt();
            int k = reader.nextInt();
            long[] a = {a0, a1};
            for (int i = 2; i <= k; ++i) {
                long tmp = p*a[1]%10000 + q*a[0]%10000; 
                a[0] = a[1];
                a[1] = tmp;
            }
            System.out.println(a[1]%10000);
        }
    }
}

 

JAVA语法——递推数列

标签:for   question   tle   term   www.   scan   can   nbsp   static   

原文地址:https://www.cnblogs.com/JAYPARK/p/10105495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!