序列中最大的数(51Nod-1062)
生活随笔
收集整理的這篇文章主要介紹了
序列中最大的数(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];elsea[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; }?
總結
以上是生活随笔為你收集整理的序列中最大的数(51Nod-1062)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线性结构 —— 差分数组
- 下一篇: 理论基础 —— 栈 —— 顺序栈