题目描述:
A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.
How many pairs of consequitive zeroes will appear in the sequence after n steps?
输入描述:
Every input line contains one natural number n (0 < n ≤1000).
输出描述:
For each input n print the number of consecutive zeroes pairs that will appear in the sequence after n steps.
输入:
2
3
输出:
1
1
题意:
电脑里面存了一个初始的数字1,后面的所有规律和操作都从1开始,然后每一次产生一个变换,变换的法则如下即 在这个数列中所有的1变换成01,所有的0变换成10,这样说可能还不明确,从1 开始,下一次则得到01(1->01),再下次则得到1001(上一次的0->10,1->01)然后得到01101001,然后依次类推…问你经过n次变换有多少组00
题解:
b[n]=2*b[n-2]+b[n-1]
代码:
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
BigInteger a[] = new BigInteger[1001];
BigInteger b[] = new BigInteger[1001];
a[0] = BigInteger.ONE;
b[2] = BigInteger.ONE;
b[0] = BigInteger.ZERO;
b[1] = BigInteger.ZERO;
for (int i = 1; i < 1001; i++)
{
a[i] = a[i - 1].multiply(BigInteger.valueOf(2));
if (i >= 3)
{
b[i] = a[i - 3].add(b[i - 2]);
}
}
while (input.hasNext())
{
int n = input.nextInt();
System.out.println(b[n]);
}
}
}