序列中最大的数(51Nod-1062)

题目

有这样一个序列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。

输入

第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10)
第2 - T + 1行:T个数,表示需要计算的n。(1 <= n <= 10^5)

输出

共T行,每行1个最大值。

输入样例

2
5
10

输出样例

3
4

思路:根据规律将数组 a[] 打个表,在打表的过程中顺便把前 n 个数的最大值求了,最后直接根据查询输出即可

源程序

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define E 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=7;
const int N=100000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;
int a[N];
int res[N];
void init(){
    a[0]=0;
    a[1]=1;

    res[0]=0;
    res[1]=1;

    int maxx=-INF;
    for(int i=2;i<=1E5;i++){
        if(i%2==0)
            a[i]=a[i/2];
        else
            a[i]=a[(i-1)/2]+a[(i-1)/2+1];

        res[i]=max(res[i-1],a[i]);
    }
}
int main(){
    init();
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        printf("%d\n",res[n]);
    }
    return 0;
}

 

posted @   老程序员111  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示