poj2680 Computer Transformation----简单的递推

Computer Transformation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4120 Accepted: 1592

Description

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?

Input

Every input line contains one natural number n (0 < n <= 1000).

Output

For each input n print the number of consequitive zeroes pairs that will appear in the sequence after n steps.

Sample Input

2
3

Sample Output

1
1

Source

 
题意:开始的数字是1,每次1变为01,0变为10,问经过n步后,有多少对连续的0,即多少对00.
打表很容易发现规律。
要用高精。
打表的代码:
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
using namespace std;
int s1[1000];
int s2[1000];
int main()
{
    int i=0;
    memset(s1,-1,sizeof(s1));
    memset(s2,-1,sizeof(s2));
    s1[0]=1;
    while(i<10)
    {
        i++;
        int cc=0;
        for(int j=0;;j++)
        {
            if(s1[j]!=-1)
            {
                if(s1[j]==0) {s2[cc++]=1;s2[cc++]=0;}
                else {s2[cc++]=0;s2[cc++]=1;}
            }
            else break;
        }
        /*for(int j=0;j<cc;j++)
        cout<<s2[j];
        cout<<"*"<<endl;*/
        int count=0;
        for(int j=0;j<cc-1;j++)
        {
            if(s2[j]==0&&s2[j+1]==0)
            count++;
        }
        cout<<i<<" "<<count<<endl;
        for(int j=0;j<cc;j++)
        s1[j]=s2[j];
    }
}

最后实现的代码:
import java.util.*;
import java.math.*;
public class Main{
public static BigInteger[] p=new BigInteger[1010];
public static BigInteger[] s=new BigInteger[1010];
public static void init()
{
	BigInteger a=BigInteger.valueOf(0);
	BigInteger b=BigInteger.valueOf(1);
	p[1]=a;p[2]=b;s[2]=b;
	for(int i=3;i<=1000;i++)
	{
		if(i%2==0)
	    {
			p[i]=s[i-1];
			p[i]=p[i].add(b);
	    }
		else
		p[i]=s[i-1];
		s[i]=s[i-1];
		//BigInteger ss=p[i];
		s[i]=s[i].add(p[i]);
	}
}
 public static void main(String[] args) {
	 init();
 Scanner cin  = new Scanner(System.in);
  while(cin.hasNext())
  {
	  int n=cin.nextInt();
	  System.out.println(p[n]);
  }
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值