本题总分:10 分 【问题描述】 给定数列 1, 1, 1, 3, 5, 9, 17, …,从第 4 项开始,每项都是前 3 项的和。求 第 20190324 项的最后 4 位数字。 【答案提交】 这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个 4 位整数(提示:答案的千位不为 0),在提交答案时只填写这个整数,填写 多余的内容将无法得分。
法一:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int[] a = new int[90000000];
a[0] = 1;
a[1] = 1;
a[2] = 1;
for (int i = 3; i < 20190324; i++) {
a[i] = (a[i - 1] + a[i - 2] + a[i - 3]) % 10000;
}
System.out.println(a[20190323]);//注意这里是要求的数的前一位应为是下标。
}
}
法二:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int a = 1;
int b = 1;
int c = 1;
for (int i = 3; i < 20190324; i++) {
int temp = (a + b + c) % 10000;
a = b;
b = c;
c = temp;
}
System.out.println(c);
}
}