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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

完美数列【置顶】

發布時間:2024/4/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 完美数列【置顶】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:http://www.nowcoder.com/questionTerminal/5bed191944ce4eaa93f4bae50abc90df

題目描述

給定一個正整數數列,和正整數p,設這個數列中的最大值是M,最小值是m,如果M <= m * p,則稱這個數列是完美數列。



現在給定參數p和一些正整數,請你從中選擇盡可能多的數構成一個完美數列。

?

輸入描述:

輸入第一行給出兩個正整數N和p,其中N(<= 105)是輸入的正整數的個數,p(<= 109)是給定的參數。第二行給出N個正整數,每個數

不超過109。



輸出描述:

在一行中輸出最多可以選擇多少個數可以用它們組成一個完美數列。

?

輸入例子:

10 8

2 3 20 4 5 1 6 7 8 9

?

輸出例子:

8

思路:題意很簡單,但是真要考慮效率的話還需要好好想想辦法。AC代碼是看了別人的,想法很好,認真學習! 1 AC代碼: 2 #include "iostream" 3 #include <cstdio> 4 #include <string.h> 5 #include <string> 6 #include <cmath> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 100005; 11 int num[MAXN]; 12 13 14 int main() 15 { 16 int n, p, i, j; 17 scanf("%d%d", &n, &p); 18 for(i=1; i<=n; ++i) 19 { 20 scanf("%d", &num[i]); 21 } 22 sort(num+1, num+n+1); 23 int len=0, flag=0; 24 for(int i=1; i<=n; ++i) 25 { 26 for(int j=i+len; j<=n; ++j) 27 { 28 if(num[j] > num[i]*p) 29 { 30 break; 31 } 32 if(j-i+1 > len) 33 { 34 len = j-i+1; 35 } 36 } 37 } 38 cout <<len <<endl; 39 return 0; 40 } 1 TLE代碼: 2 #include "iostream" 3 #include <iomanip> 4 #include <string.h> 5 #include <string> 6 #include <vector> 7 #include <cmath> 8 #include <cctype> 9 #include <algorithm> 10 using namespace std; 11 12 const int MAXN = 100005; 13 int num[MAXN]; 14 15 int main() 16 { 17 int n, p, i, j; 18 //cin >>n >>p; 19 scanf("%d%d", &n, &p); 20 for(i=1; i<=n; ++i) 21 { 22 //cin >>num[i]; 23 scanf("%d", &num[i]); 24 } 25 sort(num+1, num+n+1); 26 int len=0, flag=0; 27 for(int i=n; i>=1; --i) 28 { 29 for(int j=1; j<=n-i+1; ++j) 30 { 31 if(num[j+i-1] <= p*num[j]) 32 { 33 len = i; 34 flag = 1; 35 break; 36 } 37 } 38 if(flag == 1) 39 { 40 break; 41 } 42 } 43 cout <<len <<endl; 44 return 0; 45 }

?

轉載于:https://www.cnblogs.com/mtc-dyc/p/4626262.html

總結

以上是生活随笔為你收集整理的完美数列【置顶】的全部內容,希望文章能夠幫你解決所遇到的問題。

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