题解 | #互换最大最小数#
互换最大最小数
https://www.nowcoder.com/practice/04c0f21530994fbfa94887f1e3588e6a
#include <bits/stdc++.h> using namespace std; int main() { int n;cin>>n; int maxNum=INT_MIN, minNum=INT_MAX, index=0, maxIndex=-1, minIndex=-1, temp; vector<int>v; while(n--){ cin>>temp; if(temp>maxNum){ maxNum = temp; maxIndex = index; } if(temp<minNum){ minNum = temp; minIndex = index; } v.push_back(temp); index++; } swap(v[maxIndex],v[minIndex]); for(auto a:v) cout<<a<<" "; } // 64 位输出请用 printf("%lld")
qd