• (大数)Computer Transformation hdu1041


    Computer Transformation

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    Total Submission(s): 8688    Accepted Submission(s): 3282

    Problem 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 consecutive zeroes pairs that will appear in the sequence after n steps.

    Sample Input

    2

    3

    Sample Output

    1

    1

    java,递推

    递推:0->10  ;

             1->01;

             00->1010;

             10->0110;

              01->1001;

              11->0101;

    假设a[i]表示第i 步时候的00的个数,由上面的可以看到,00是由01 得到的,所以只要知道a[i-1]的01的个数就能够知道a[i]的00的个数了,那a[i-1]怎么求呢,同样看推导,01由1和00 得到,而第i步1的个数是2^(i-1),所以a[i]=2^(i-3)+a[i-2];(最后计算的是第i-2步情况)。

    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            BigInteger a[]=new BigInteger[1001];
            while(in.hasNextInt()) {
                int n=in.nextInt();
                a[1]=BigInteger.valueOf(0);
                a[2]=BigInteger.valueOf(1);
                a[3]=BigInteger.valueOf(1);
                for(int i=4;i<=n;i++) {
                    a[i]=BigInteger.valueOf(0);      //先进行初始化。
                    int m=i-3;        //在大数的pow(m,n)中,n是int类型的,m是BigInteger类型的。             
                    BigInteger q= new BigInteger("2");
                    a[i]=a[i].add(q.pow(m));
                    a[i]=a[i].add(a[i-2]);
                }
                System.out.println(a[n]);
            }
        }
    }
    View Code
  • 相关阅读:
    django补充
    python自动化开发-[第二十五天]-scrapy进阶与flask使用
    python自动化开发-[第二十四天]-高性能相关与初识scrapy
    python自动化开发-[第二十三天]-初识爬虫
    python自动化开发-[第二十二天]-bbs多级评论、点赞、上传文件
    django模版之过滤器
    django_admin用法
    Django----配置数据库读写分离
    Flask解决跨域
    MongoDB
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/9166488.html
Copyright © 2020-2023  润新知