目录

​1,题目描述​

​题目大意​

​2,思路​

​3,AC代码​

​4,解题过程​

​第一搏​

​第二搏​

​第三搏​

​ 第四搏​

 

 


1,题目描述

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_甲级

Sample Input:

10
6 7 6 9 3 10 8 2 7 8

 

Sample Output:

6

题目大意

求爱丁顿数。

the maximum integer E such that it is for E days that one rides more than E miles:有E天骑行超过E公里

2,思路

(虽说是数学题,但更像是找规律。。。)

参考​​@日沉云起【pat甲级1117. Eddington Number(25)、乙级1060. 爱丁顿数(25)】​

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_甲级_02

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_C++_03

3,AC代码

#include<bits/stdc++.h>
using namespace std;

int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, e = 0;
cin>>N;
int data[N];
for(int i = 0; i < N; i++){
scanf("%d", &data[i]);
}
sort(data, data + N, greater<int>());
while(e < N && data[e] > e+1) e++; // !!!是e+1
cout<<e;
return 0;
}

4,解题过程

第一搏

没想那么多,双指针法,sum > pow(right - left + 1, 2)右指针right向右移,否则左指针向右移;

#include<bits/stdc++.h>
using namespace std;

int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, data[100005];
cin>>N;
for(int i = 0; i < N; i++)
scanf("%d", &data[i]);
int E = 0, left = 0, right = 0, sum = data[0];
while(left <= right && left >= 0 && right < N){
if(sum > pow(right - left + 1, 2)){
E = max(E, right-left+1);
sum += data[right++];
}else{
sum -= data[left++];
}

}
cout<<E;
return 0;
}

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_1117_04

第二搏

题意理解错了,(原先理解为连续E天,平均每天骑行超过E英里)(;′⌒`)

看完大神的代码后才知道,不是连续的。。。

E days that one rides more than E miles:超过E英里的天数大于等于E;

于是

#include<bits/stdc++.h>
using namespace std;

int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, dis;
map<int, int> data;
cin>>N;
for(int i = 0; i < N; i++){
scanf("%d", &dis);
data[dis]++;
}
int E = 0, num = N;
for(auto it : data){
if(num >= it.first){
E = it.first;
num -= it.second;
}else break;
}
cout<<E;
return 0;
}

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_PAT_05

第三搏

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_PAT_06

下标可以看作是比距离data[i]大的天数,比如比10mile大的为0天,比9mile大的为1天,比6mile大的为7天(但取E为6)

于是

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){return a > b;}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, data[100005];
cin>>N;
for(int i = 0; i < N; i++){
scanf("%d", &data[i]);
}
sort(data, data + N, cmp);
for(int i = 0; i < N; i++){
if(i >= data[i]){
cout<<data[i];
return 0;
}
}
}

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_1117_07

 第四搏

看了大佬 的博客,,,做了以下修改(没理解为什么。。。)

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_1117_08

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a, int b){return a > b;}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
int N, data[100005];
cin>>N;
for(int i = 0; i < N; i++){
scanf("%d", &data[i]);
}
sort(data, data + N, cmp);
for(int i = 0; i < N; i++){
if(data[i] <= i+1){
cout<<i;
return 0;
}
}
}

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_1117_09

。。。

改成while循环就好了(可能是漏掉了i==N的情况)

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_甲级_10

*PAT_甲级_1117 Eddington Number (25point(s)) (C++)【找规律】_C++_11