HDOJ--1041--Computer Transformation

題目描述:
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]);
		}
	}
 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章