生活随笔
收集整理的這篇文章主要介紹了
*PAT_B_1030_Java(22分)_C++(25分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,可以想到的是將數據存儲后,從小到大排序,題目知,要求隊列的最長數據,需要最大值和最小值,則可以循環從頭和尾獲取數據,判斷是否符合標準,符合則記錄下數據個數,每一次判斷符合都與整個數據比較,最后輸出最大值。
具體大概就是兩個循環,第一個循環依次向后遍歷。第二個循環從第一次循環遍歷位置開始,從后向前匹配,比較出最大的隊列長度。
import java
.util
.Arrays
;
import java
.util
.Scanner
;public class Main {public static void main(String
[] args
) {Scanner in
=new Scanner(System
.in
);int N
=in
.nextInt();int p
=in
.nextInt();long num
[]=new long[N
];for(int i
=0;i
<N
;i
++) {num
[i
]=in
.nextLong();}in
.close();int max
=0;int count
=0;Arrays
.sort(num
);for(int i
=0;i
<N
;i
++) {if(max
>N
-i
+1)break;for(int j
=i
;j
<N
;j
++) {if(max
>N
-j
+1)break;int temp
=N
-1;if(num
[i
]*p
<num
[temp
])temp
--;elseif(max
<temp
-j
+1)max
=temp
-j
+1;}}System
.out
.print(max
);}
}
import java
.util
.ArrayList
;
import java
.util
.Collections
;
import java
.util
.Scanner
;public class Main {public static void main(String
[] args
) {Scanner sc
= new Scanner(System
.in
);int N
= sc
.nextInt();double p
= sc
.nextInt();ArrayList
<Integer> list
= new ArrayList<Integer>();for (int i
= 0; i
< N
; i
++) {list
.add(sc
.nextInt());}sc
.close();Collections
.sort(list
);int count
= 0;for (int i
= 0; i
< N
; i
++) {for (int j
= i
+count
; j
< N
; j
++) {if(list
.get(j
) > list
.get(i
)*p
) break;if( j
-i
+1 > count
) count
= j
-i
+1;}}System
.out
.print(count
);}
}
#include <iostream>
#include <algorithm>
#define MAXSIZE 100000
using namespace std
;int main()
{int max
=0,count
=0,N
,p
;cin
>>N
>>p
;long nums
[MAXSIZE
];int i
;for(i
=0;i
<N
;i
++){scanf("%ld",&nums
[i
]);}sort(nums
,nums
+N
);int j
=0;for(i
=0;i
<N
;i
++){count
=0;while(nums
[j
]<=(nums
[i
]*p
)&&j
<N
) j
++;count
=j
-i
;if(count
>max
)max
=count
;}cout
<<max
;return 0;
}
總結
以上是生活随笔為你收集整理的*PAT_B_1030_Java(22分)_C++(25分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。