poj 1953 World Cup Noise

题目链接:http://poj.org/problem?id=1953

题目大意:给定一个小于 45 的整数 n, 求 n 位 2 进制数中不含相邻 1 的数的个数。

解题思路:记 n 位 2 进制数中不含相邻 1 的数的个数为 F[n], 以其最后一位的数字进行分类处理:

① 最后一位为 0, 则前 n-1 个数字只要合法就满足条件,此时有 F[n-1] 种。

② 最后一位为 1, 则倒数第二位必须为 0, 其前 n-2 个数字只要合法就满足条件,此时有 F[n-2] 种。

所以,F[n]=F[n-1]+F[n-2] (n≥3), 容易求得 F[1]=2, F[2]=3. (由于 F[44] 在 int 范围内,故只需用 int 存储就行)

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: poj 1953
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 typedef long long LL;
26 const double PI=acos(-1.0);
27 ///////////////////////////////////////////////////////////////////////////
28 
29 ///////////////////////////////////////////////////////////////////////////
30 //Add Code:
31 ///////////////////////////////////////////////////////////////////////////
32 
33 int main(){
34     ///////////////////////////////////////////////////////////////////////
35     //Add code:
36     int T,n,i,F[50];
37     F[1]=2,F[2]=3;
38     for(i=3;i<45;i++) F[i]=F[i-1]+F[i-2];
39     scanf("%d",&T);
40     for(i=1;i<=T;i++){
41         scanf("%d",&n);
42         if(i>1) printf("\n");
43         printf("Scenario #%d:\n",i);
44         printf("%d\n",F[n]);
45     }
46     ///////////////////////////////////////////////////////////////////////
47     return 0;
48 }
49 
50 ///////////////////////////////////////////////////////////////////////////
51 /*
52 Testcase:
53 Input:
54 2
55 3
56 1
57 Output:
58 Scenario #1:
59 5
60 
61 Scenario #2:
62 2
63 */
64 ///////////////////////////////////////////////////////////////////////////

posted on 2013-08-19 11:15  SCNU20102200088  阅读(263)  评论(0编辑  收藏  举报

导航