zoukankan      html  css  js  c++  java
  • PAT A1117 Eddington Number(25) [简单逻辑题]

    题目

    British astronomer Eddington liked to ride a bike. It is said that in order to show of his skill, he has even defined an “Eddington number”, E — that is, the maximum integer E such that it is for E days that one rides more than E miles. Eddington’s own E was 87. Now given everyday’s distances that one rides for N days, you are supposed to find the corresponding E (<=N).
    Input Specification:
    Each input file contains one test case. For each case, the first line gives a positive integer N(<=105), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.
    Output Specification:
    For each case, print in a line the Eddington number for these N days.
    Sample Input:
    10
    6 7 6 9 3 10 8 2 7 8
    Sample Output:
    6

    题目分析

    求n个数中,e个大于e的数,要求e最大

    解题思路

    接收输入,倒序排序,遍历比较rec[i]>i+1的个数

    易错点

    1. 此题若没有排序,直接遍历数组,判断rec[i]>i+1计数,得24分,这种理解错误。如:5 1 2 3 4 5 5 5 5 按照此种理解计算结果为1,但是此系列正确结果为8个数中 有4个大于4的数,且4为最大e,没有5个大于5的数

    知识点

    sort排序中,int升序比较器可使用greater();greater和less是xfunctional.h中的两个结构体

    #include<iostream>
    #include<algorithm>//因为用了sort()函数
    #include<functional>//因为用了greater<int>()
    using namespace std;
    void main(){
          int a[]={3,1,4,2,5};
          int len=sizeof(a)/sizeof(int);//这里切记要除以sizeof(int)!
          sort(a,a+len,greater<int>());//内置类型的由大到小排序
          for(int i=0;i<len;i++)
          cout<<a[i]<<" ";
          return 0;
    }
    

    code

    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn=100010;
    int rec[maxn];
    int main(int argc,char * argv[]) {
    	int n,e =0;
    	scanf("%d",&n);
    	for(int i=0; i<n; i++)
    		scanf("%d",&rec[i]);
    	sort(rec,rec+n,greater<int>());
    	while(e<n&&rec[e]>e+1)e++;
    	printf("%d",e);
    	return 0;
    }
    
  • 相关阅读:
    km算法
    HDU 1358
    HDU 3746
    CF 432D
    HDU 4725
    14年百度之星资格赛第四题
    AC自动机
    RMQ
    HDU 4635
    HDU 3667
  • 原文地址:https://www.cnblogs.com/houzm/p/12852858.html
Copyright © 2011-2022 走看看