【入门】换位置暂无标签时间限制:CC++ 1000MS其他语言 2000MS内存限制:CC++ 16MB其他语言 32MB难度:中等出题人:描述体育课上有一个班级的同学站成了一队体育老师请最高的和最矮的两位同学调换一下位置其余的同学不要动请编程实现!假设所有人的高矮都是不一样的输入描述第一行有一个整数n代表该班级的总人数n≤100第二行有n个数代表每个人的身高输出描述调换位置后的结果用例输入 1
#include
int main() { int n; std::cin >> n;
std::vector<int> heights(n);
for (int i = 0; i < n; i++) {
std::cin >> heights[i];
}
int minIndex = std::min_element(heights.begin(), heights.end()) - heights.begin();
int maxIndex = std::max_element(heights.begin(), heights.end()) - heights.begin();
std::swap(heights[minIndex], heights[maxIndex]);
for (int i = 0; i < n; i++) {
std::cout << heights[i] << " ";
}
return 0;
原文地址: https://www.cveoy.top/t/topic/iEV7 著作权归作者所有。请勿转载和采集!