日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/17 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 序列中最大的数(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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。