题目描述
上机练习5.1.3 输入N个整数,找出最大数所在位置,并将它与第一个数对调位置。
输入
第一行,整数N,1≤N≤104;
第二行,N个不超过104的整数。
输出
第一行,最大数的位置,如果有多个,输出最前面的位置;
第二行,N个整数,最大数与第一个数对调后按顺序输出,中间用空格分隔,行尾不能有空格。
样例输入
复制
10
1 2 3 4 5 10 9 8 7 6
样例输出
复制
6
10 2 3 4 5 1 9 8 7 6
点个赞吧~
上代码!
#include <iostream>
using namespace std;
int a[10001];
int main() {
int n, max, pmax, t;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
max = a[1], pmax = 1;
for (int i = 2; i <= n; i++) {
if (a[i] > max) {
max = a[i];
pmax = i;
}
}
t = a[1];
a[1] = a[pmax];
a[pmax] = t;
cout << pmax << endl;
cout << a[1];
for (int i = 2; i <= n; i++) {
cout << " " << a[i];
}
return 0;
}
点个赞吧,还有关注😘☆*: .。. o(≧▽≦)o .。.:*☆