zoukankan      html  css  js  c++  java
  • 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("
    ");
    43         printf("Scenario #%d:
    ",i);
    44         printf("%d
    ",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 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    在像Angular2这样的SPA应用中使用Google Analytics的方法
    英语
    教你打包Java程序,jar转exe随处可跑
    [java]java字符串如何保存到数据库日期类型的列?
    获取主板序列号 cpu序列号,硬盘序列号,MAC地址
    java获取mac地址,ip地址
    java 获取硬件信息
    获取年月日 时分秒
    这是一页纸商业计划书 (Business Plan) 模板。
    javac和java命令的详解
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3267441.html
Copyright © 2011-2022 走看看