题解 | #求最大最小数#

求最大最小数

https://www.nowcoder.com/practice/82e5ff335eeb486aab359767895cc7b4

排序最小的时间复杂度为O(nlogn);本题只需在一趟遍历中维护最大最小值即可,时间复杂度为O(n)

#include <climits>
#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin >>n) { // 注意 while 处理多个 case
        int maxNum=INT_MIN;
        int minNum=INT_MAX;
        while(n--){
            int temp;
            cin>>temp;
            maxNum=max(maxNum,temp);
            minNum=min(minNum,temp);
        }
        cout<<maxNum<<' '<<minNum<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 10:56
点赞 评论 收藏
分享
07-04 09:21
已编辑
Java
推拿大师:这是hr发的钓鱼贴吗
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务