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

来源:互联网 发布:马云大数据时代演讲 编辑:程序博客网 时间:2024/06/06 18:42


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
311111111
 

Sample Output
128

分析(转):

    简单递推+大数。
    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());      }   }}



2 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 使用虚假广告词被工商查到怎么办 为什么手机淘宝店关注不了怎么办 淘宝买东西付钱后卖家不发货怎么办 微信销售群没人买东西怎么办 淘宝想开2个店铺怎么办 拼多多没钱交保证金怎么办 差评一个月后怎么办 淘宝买家投诉成立后怎么办 天猫投诉成立了怎么办 新开实体店铺没生意怎么办 新开淘宝没流量怎么办 白色板鞋有青草弄的绿色怎么办 淘宝租衣服不退押金怎么办 c盘不可以扩展怎么办 实践教学管理平台忘记账号怎么办 淘宝申请售后店家不处理怎么办 全民k歌地区限制怎么办 网络电视hdp无法下载怎么办 淘宝店铺销量为零怎么办 快递号填错卖家拒绝退款申请怎么办 淘宝店代购 售假怎么办 淘宝申请售后卖家没钱怎么办 淘宝禁止评价一个月怎么办 被卖家电话骚扰怎么办 恶意骚扰扣12分怎么办 新店开张交保证金被骗了怎么办 支付宝蚂蚁花呗逾期怎么办 被注销的微信怎么办 花呗有些不能用怎么办 实体店生意不好做怎么办 电器实体店生意越来越差怎么办 开业第一天不吉利怎么办 美容店开业第一天没人怎么办 淘宝店铺没有人访问怎么办 淘宝店铺没有人问怎么办 淘宝申请退款后店铺关闭怎么办 宝贝详情怎么改不了怎么办 改详情页后被删除宝贝怎么办 淘宝网商贷生意不好还不了怎么办 英国遗失在酒店物品怎么办 班福法则首位是0怎么办