码迷,mamicode.com
首页 > 其他好文 > 详细

ACM--递推加大数--HDOJ 1865--1string--水

时间:2016-07-22 19:10:34      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:


HDOJ题目地址:传送门


1sting

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5135    Accepted Submission(s): 1926


Problem Description
You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or leave the ‘1’ there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find the total number of result you can get.
 

Input
The first line is a number n refers to the number of test cases. Then n lines follows, each line has a string made up of ‘1’ . The maximum length of the sequence is 200.
 

Output
The output contain n lines, each line output the number of result you can get .
 

Sample Input
3 1 11 11111
 

Sample Output
1 2 8

分析(转):

    简单递推+大数。
    f[n]=f[n-1]+f[n-2]。
    由于数字只有‘1‘和‘2‘这两种,那么当让第n
位为1的时候,f[n]加上f[n-1];当让最后两位
合并成2的时候,f[n]加上f[n-2](因为要保证
倒数第二位一定是1)。

    算出来的f就是一个斐波那契数列。



import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
    //f(n)=f(n-1)+f(n-2)
      Scanner cin= new Scanner(System.in);
      int n=cin.nextInt();
      BigDecimal []big=new BigDecimal[300];
      big[1]=new BigDecimal(1);
      big[2]=new BigDecimal(2);
      for(int i=3;i<300;i++){
          big[i]=big[i-1].add(big[i-2]);
      }
      while(n-->0){
          String s=cin.next();
          int len=s.length();
          System.out.println(big[len].toPlainString());
      }
   }
}



ACM--递推加大数--HDOJ 1865--1string--水

标签:

原文地址:http://blog.csdn.net/qq_26891045/article/details/51993763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!