

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
while(cin >> n && n!=0)
{
int a[n];
for(int i=0; i<n; i++)
{
cin >> a[i];
}
int index = 0; //最小值所对应的下标
for(int i=1; i<n; i++)
{
if(a[i] < a[index])
{
index = i;
}
}
//交换
swap(a[0], a[index]);
//控制输出
bool flag = true;
for(auto x : a)
{
if(flag)
{
cout << x;
flag = false;
}
else
{
cout << " " <<x;
}
}
cout << endl;
}
return 0;
}
本文展示了一个使用C++编写的程序,该程序能够读取一组整数,并找到其中的最小值,然后将最小值与数组的第一个元素进行交换。通过遍历数组并比较每个元素,确定最小值及其位置,最后通过std::swap函数实现元素交换。
2万+

被折叠的 条评论
为什么被折叠?



