PAT 1060 乙等 (爱丁顿数) c++

来源:互联网 发布:linux 运行lua文件 编辑:程序博客网 时间:2024/06/05 17:56

思路: 先将n天骑车的距离升序排序, 查找第一个满足小于等于n天的骑车距离,排序后的数组末尾开始遍历求解。

#include <iostream>#include <algorithm>using namespace std;/*另加测试用例:1. 3   1 2 32. 3    0 1 13. 5   5 5 5 6 64. 5   5 5 5 5 5*/int main(){     int n(0);  //连续骑车天数    cin >> n;    int *a = new int[n];    for (int i = 0; i < n; i++){        cin >> a[i];    }    sort(a, a + n);    /*    for (int i = n - 1; i>-0; i--){        if (a[i] <= n && i>=1){            if ((n - i) == a[i-1]){                cout << a[i-1];                break;            }        }    }    */    int temp;  //当汽车距离大于骑车总天数n时,肯定不满足,因此,查找  第一个小于等于n的骑车距离temp    for (int i = a[n - 1]; i >= 0; i--){        if (i <= n){            temp = i;            break;        }    }    for (int i = temp; i >= 0; i--){        int num(0);        for (int j = n - 1; j >= 0; j--){            if (a[j] > i){                num++;            }            else{                break;            }        }        if (num >= i){            cout << i;            break;        }        /*        if (i == 1){            cout << i;            break;        }        */    }    return 0;}
0 0
原创粉丝点击