World Cup Noise
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16774   Accepted: 8243

Description

Background 
"KO-RE-A, KO-RE-A" shout 54.000 happy football fans after their team has reached the semifinals of the FIFA World Cup in their home country. But although their excitement is real, the Korean people are still very organized by nature. For example, they have organized huge trumpets (that sound like blowing a ship's horn) to support their team playing on the field. The fans want to keep the level of noise constant throughout the match. 
The trumpets are operated by compressed gas. However, if you blow the trumpet for 2 seconds without stopping it will break. So when the trumpet makes noise, everything is okay, but in a pause of the trumpet,the fans must chant "KO-RE-A"! 
Before the match, a group of fans gathers and decides on a chanting pattern. The pattern is a sequence of 0's and 1's which is interpreted in the following way: If the pattern shows a 1, the trumpet is blown. If it shows a 0, the fans chant "KO-RE-A". To ensure that the trumpet will not break, the pattern is not allowed to have two consecutive 1's in it. 
Problem 
Given a positive integer n, determine the number of different chanting patterns of this length, i.e., determine the number of n-bit sequences that contain no adjacent 1's. For example, for n = 3 the answer is 5 (sequences 000, 001, 010, 100, 101 are acceptable while 011, 110, 111 are not).

Input

The first line contains the number of scenarios. 
For each scenario, you are given a single positive integer less than 45 on a line by itself.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the number of n-bit sequences which have no adjacent 1's. Terminate the output for the scenario with a blank line.

Sample Input

2
3
1

Sample Output

Scenario #1:
5 Scenario #2:
2

分析:result[i]存储 i位二进制数中没有相邻的1的个数。计算result[i],如果在result[i-1]中的各个数后加0,则都满足条件,如果在result[i-1]中的各个数后加1,要满足条件则result[i-1]中的数的最后一位必须是0,即是result[i-2]中各个数加0后的结果。所以

  

result[i] = result[i - 1] + result[i - 2]

其中result[1] 等于2,result[2]等于3,然后就是个斐波那契数列。

Java AC 代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testNum = sc.nextInt();
int[] result;
for(int i = 1; i <= testNum; i++) {
int bitNum = sc.nextInt();
result = new int[bitNum + 1];
if(bitNum == 1) {
System.out.println("Scenario #" + i + ":");
System.out.println(2);
System.out.println("");
continue;
}
result[1] = 2;
result[2] = 3;
for(int j = 3; j <= bitNum; j++) {
result[j] = result[j - 1] + result[j - 2];
}
System.out.println("Scenario #" + i + ":");
System.out.println(result[bitNum]);
System.out.println("");
}
}
}

poj 1953 World Cup Noise (dp)的更多相关文章

  1. POJ 1953 World Cup Noise

    World Cup Noise Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14397   Accepted: 7129 ...

  2. Poj 1953 World Cup Noise之解题报告

    World Cup Noise Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16369   Accepted: 8095 ...

  3. poj - 1953 - World Cup Noise(dp)

    题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:id=1953" target="_blank">h ...

  4. POJ 1953 World Cup Noise(递推)

    https://vjudge.net/problem/POJ-1953 题意:输入一个n,这n位数只由0和1组成,并且不能两个1相邻.计算共有多少种排列方法. 思路:递推题. 首先a[1]=2,a[2 ...

  5. POJ-1953 World Cup Noise(线性动规)

    World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16374 Accepted: 8097 Desc ...

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  8. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

  9. poj 3254(状态压缩DP)

    poj  3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...

随机推荐

  1. javascript控制流程语句

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. centos的KVM初级安装

    什么是KVM虚拟化技术?KVM(Kernel-based Virtual Machine),主流虚拟化技术之一,集成与Linux2.6之后版本中,通过linux内核提供任务调度及管理.kvm,在实现虚 ...

  3. 阶段3 3.SpringMVC·_06.异常处理及拦截器_2 SpringMVC异常处理之演示程序异常

    原来的index.jsp删除.新建一个 创建pages文件夹.再创建success.jsp页面 重新部署项目 把这个项目移除掉 加入新的项目 启动tomcat服务器 模拟异常 方法抛出异常给前端控制器 ...

  4. 五十一:数据库之Flask-Migrate详解

    在实际开发中,经常会发生数据库修改行为,一般数据库修改不是直接手动修改,而是去修改ORM模型,然后再把模型映射到数据库中,这些操作可以通过flask-migrate实现,flask-migrate是基 ...

  5. gitlab仓库的使用

    一.gitlab简介 gitlab是一个用于仓库管理系统的开源项目,使用git作为代码管理工具,并在此基础上搭建web服务. [管理命令] gitlab-ctl stop        gitlab- ...

  6. java.lang.Double[] 转 double[]

    Double[] v = list.stream().map(item -> item.getRespTime()).toArray(Double[]::new); Mean mean = ne ...

  7. MUT值设置、top等命令无法执行、ssh无法登陆、vim命令卡住

    [root@host---- ~]# ifconfig eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu inet 10.1.1.204 n ...

  8. ubuntu中配置jdk1.8

    方法/步骤   1 首先,百度搜索jdk,选择第一个,网站是Oracle Jdk.点击进去 步骤阅读 2 点击Download,到官网下载linux版本的jdk.选择自己对应的操作系统及32或64位版 ...

  9. Matlab中的eig函数和Opecv中eigen()函数的区别

    奇异值分解的理论参见下面的链接 http://www.cnblogs.com/pinard/p/6251584.html https://blog.csdn.net/shenziheng1/artic ...

  10. 从内存上看python的对象

    python中有一个说法:一切皆是对象,怎么理解这句话呢?我们可以通过查看数字,字符串在内存中的表示形式来对这句话有个更深的认识. 那么,怎么查看对象在内存中是什么样的呢?可以先参考一些这篇文章:ht ...