zoukankan      html  css  js  c++  java
  • 1062 序列中最大的数

    题目来源: Ural 1079
    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
     收藏
     关注
    有这样一个序列a:
    a[0] = 0
    a[1] = 1
    a[2i] = a[i]
    a[2i+1] = a[i] + a[i+1]
     
    输入一个数N,求a[0] - a[n]中最大的数。
    a[0] = 0, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 1, a[5] = 3, a[6] = 2, a[7] = 3, a[8] = 1, a[9] = 4, a[10] = 3。
    例如:n = 5,最大值是3,n = 10,最大值是4。
     
    Input
    第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10)
    第2 - T + 1行:T个数,表示需要计算的n。(1 <= n <= 10^5)
    Output
    共T行,每行1个最大值。
    Input示例
    2
    5
    10
    Output示例
    3
    4

    打表。。
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <fstream>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <map>
    #include <set>
    #include <iomanip>
    using namespace std;
    #define MAXN 100005
    typedef long long LL;
    LL a[MAXN],m[MAXN];
    /*
    a[0] = 0
    a[1] = 1
    a[2i] = a[i]
    a[2i+1] = a[i] + a[i+1]
    0 1 2 3 4 5 6 7 8 9 10
    0 1 1 2 1 3 2 3 1 4 3
    */
    void init()
    {
        a[0] = m[0] = 0;
        a[1] = m[1] = 1; 
        LL M = 1;
        for(LL i=2;i<MAXN;i++)
        {
            if(i%2==0)
            {
                a[i] = a[i/2];
            }
            else
            {
                a[i] = a[i/2]+a[i/2+1];
            }
            M = max(M,a[i]);
            m[i] = M;
        }
    }
    int main()
    {
        init();
        LL t,n;
        scanf("%lld",&t);
        while(t--)
        {
            scanf("%lld",&n);
            printf("%lld
    ",m[n]);
        }
        return 0;
    }
  • 相关阅读:
    Django_Setings
    python之event【事件】
    python之递归锁【Rlock】
    python之信号量【Semaphore】
    python的线程锁
    python的多线程和守护线程
    python的错误类型和异常处理
    python之ftp作业【还未完成】
    python之socket运用之传输大文件
    python之socket运用之执行命令
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6275192.html
Copyright © 2011-2022 走看看