文章

33

粉丝

0

获赞

151

访问

5.3k

头像
删除最大最小数 题解:熟悉STL容器
P1022 贵州大学机试题
发布于2026年3月20日 17:01
阅读数 110

#include<bits/stdc++.h>

using namespace std;


int main() {
    list<int> lst;
    
    int n;
    cin>>n;
    while(n--){
    	int temp;
    	cin>>temp;
    	lst.push_back(temp);
	}
	if(lst.empty()) return 0;
	
    //小众找最值的函数,真是第一次用
	int max_val = *max_element(lst.begin(),lst.end());
	int min_val = *min_element(lst.begin(),lst.end());
	
    //指针指向这个值的结点
	auto it = find(lst.begin(),lst.end(),max_val);
	if(it != lst.end())lst.erase(it);
	
	it = find(lst.begin(),lst.end(),min_val);
	if(it != lst.end())lst.erase(it);
	  
	for(int val:lst){cout<<val<<" ";
	}
	  
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发