poj 1953 World Cup Noise (dp)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 16774 | Accepted: 8243 |
Description
"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
For each scenario, you are given a single positive integer less than 45 on a line by itself.
Output
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)的更多相关文章
- POJ 1953 World Cup Noise
World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14397 Accepted: 7129 ...
- Poj 1953 World Cup Noise之解题报告
World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16369 Accepted: 8095 ...
- poj - 1953 - World Cup Noise(dp)
题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:id=1953" target="_blank">h ...
- POJ 1953 World Cup Noise(递推)
https://vjudge.net/problem/POJ-1953 题意:输入一个n,这n位数只由0和1组成,并且不能两个1相邻.计算共有多少种排列方法. 思路:递推题. 首先a[1]=2,a[2 ...
- POJ-1953 World Cup Noise(线性动规)
World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16374 Accepted: 8097 Desc ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- poj 3311(状态压缩DP)
poj 3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...
- poj 1185(状态压缩DP)
poj 1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...
- poj 3254(状态压缩DP)
poj 3254(状态压缩DP) 题意:一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相 ...
随机推荐
- 客户端连接oracle11出现提示ORA-12514:错误解决方法
近来安装oracle11g,使用后发现plsql和sqldeveloper等客户端工具不能用,提示以下错误: 1.ORA-12514: TNS: 程序无法监听 原因:OracleOraDb11g_ho ...
- 七十五:flask.Restful之Restful.API介绍
restful api是用于在前端与后台进行通信的一套规范,使用这个规范可以让前后端开发变得更加轻松 协议:http或者https 数据传输格式:使用json url链接:url链接中不能有动词(/g ...
- 【汇总】数据库提权(mysql、mssql)
日期:2018-04-03 11:46:45 作者:Bay0net 介绍:利用 mssql 的 sa 账号提权.利用 MySQL 的 UDF 提权 0x01.mssql 提权 恢复 xp_cmdshe ...
- 走进异步编程的世界 - 在 GUI 中执行异步操作
转载:https://www.cnblogs.com/liqingwen/p/5877042.html 走进异步编程的世界 - 在 GUI 中执行异步操作 [博主]反骨仔 [原文地址]http://w ...
- java:JQuery(Ajax,JSON)
1.遍历ajax返回的json: 第一种: <%@ page language="java" import="java.util.*" pageEncod ...
- C# 线程thread
一.问题总结 1. 在WinForm开发过程中用到线程时,往往需要在线程中访问线程外的控件,比如:设置textbox的Text值等等.如果直接访问UI控件会报出“从不是创建控件的线程访问它”错误.控件 ...
- 刷新页面后,让控制台的js代码继续执行
在各种限时,秒杀活动中,有个自动循环的点击的工具是很重要的. 为了方便起见,我们把Js代码放在浏览器的控制台执行,但是刷新页面后,js代码就清空了,也就无法执行. 可以用js代码实现一个不受页面刷新影 ...
- Android开发 互相调用模式之提供扩展类
此种方法适用于:比如你要让Android做一些事情,这些事用不到任何资源,在Android下用纯代码就能实现它,这样就可以在Android下写好,将它封装成一个方法,打成包按照下面的方式丢给Unity ...
- OpenStack组件——Keystone身份认证
1.keystone介绍 keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户权限的定义等等 ...
- Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)
Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...