/*
Total Submissions: 15598 Accepted: 7691
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
Source
给定一个小于45的整数n,求n位2进制数中不含相邻1的数的个数。看似简单的一道题,如果当n=45时,对2的45次方检查,是无法完成的任务。先分析一下这个问题:
N 以1结尾的个数 以0结尾的个数 总和
1 1 1 2
2 1 2 3
3 … … …
对于n=1来说,以1结尾、以0结尾个数都是1,总和是2,下面过度到2:对于所有以1结尾的数,后面都可以加上0,变为n=2时以0结尾的,而只有结尾为0的数才能加上1(因为不能有两个连续0),这样就可以在n=2的格里分别填上1、2,总和算出来为3,以此类推,我们可以算出所有n<=45的值,然后根据输入进行相应输出。核心代码如下:
int i,num,count,array[50][2],j=0;
array[1][1] = 1;
array[1][0] = 1;
for(i=2;i<50;i++)
{
array[i][0] = array[i-1][1];
array[i][1] = array[i-1][1]+array[i-1][0];
}
我们可以继续找出规律,其实这个就是斐波那切数列数列:
F[N] = F[N-1]+F[N-2];可以继续简化代码。
*/
#include<string.h>
#include<stdlib.h>
#define M 50
__int64 a[M];
int main()
{
memset(a,0,sizeof(a));
int i=0;
a[1]=2 ;
a[2]=3;
for(i=3;i<=45;i++)
a[i]=a[i-1]+a[i-2];
int T,ca=1;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
printf("Scenario #%d:\n",ca);
printf("%I64d\n\n",a[n]);
ca++;
}
return 0;
}
World Cup Noise
http://poj.org/problem?id=1953
Total Submissions: 15598 Accepted: 7691
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
Source
*/
题意: 简而言之,一个N位的二进制数,求不出现连续2个1的序列个数
思路:
/*
给定一个小于45的整数n,求n位2进制数中不含相邻1的数的个数。看似简单的一道题,如果当n=45时,对2的45次方检查,是无法完成的任务。先分析一下这个问题:
N 以1结尾的个数 以0结尾的个数 总和
1 1 1 2
2 1 2 3
3 … … …
对于n=1来说,以1结尾、以0结尾个数都是1,总和是2,下面过度到2:对于所有以1结尾的数,后面都可以加上0,变为n=2时以0结尾的,而只有结尾为0的数才能加上1(因为不能有两个连续0),这样就可以在n=2的格里分别填上1、2,总和算出来为3,以此类推,我们可以算出所有n<=45的值,然后根据输入进行相应输出。核心代码如下:
int i,num,count,array[50][2],j=0;
array[1][1] = 1;
array[1][0] = 1;
for(i=2;i<50;i++)
{
array[i][0] = array[i-1][1];
array[i][1] = array[i-1][1]+array[i-1][0];
}
我们可以继续找出规律,其实这个就是斐波那切数列数列:
F[N] = F[N-1]+F[N-2];可以继续简化代码。
*/
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#define M 50
__int64 a[M];
int main()
{
memset(a,0,sizeof(a));
int i=0;
a[1]=2 ;
a[2]=3;
for(i=3;i<=45;i++)
a[i]=a[i-1]+a[i-2];
int T,ca=1;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
printf("Scenario #%d:\n",ca);
printf("%I64d\n\n",a[n]);
ca++;
}
return 0;
}