Number Sequence
Posted 给我一个团队,干翻TX
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Number Sequence相关的知识,希望对你有一定的参考价值。
题目是这样的:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
2 5
package package1;
import java.util.Scanner;
public class Main
{
public static int A,B,n;
public static final int Maxn = 100;
public static int[] Arr = new int[Maxn];
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
A = cin.nextInt();
B = cin.nextInt();
n = cin.nextInt();
if(A == 0 && B == 0 && n == 0)
{
return;
}
int T;
T = init();
Arr[0] = Arr[T];
System.out.println(Arr[n%T]);
}
}
public static int init()
{
Arr[1] = 1;
Arr[2] = 1;
int i;
for(i = 3 ; i < Maxn ; i++)
{
Arr[i] = (A * Arr[i-1] + B * Arr[i-2])%7;
if(Arr[i-1] == 1 && Arr[i] == 1)
{
break;
}
}
return (i - 2);
}
}
修改之后的代码实现如下:
以上是关于Number Sequence的主要内容,如果未能解决你的问题,请参考以下文章