输入N个整数,找出最大数所在的位置,并将它与第一数对调位置,输出改变后的数列

第一行输入n.第二行输入n个数。 输出改变后的数列。 编程刚入门,求带我

循环遍历就好了啊

#include <iostream>
using namespace std;
int main()
{
    int a[10000];
    int n,max = 0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i] > a[max])
            max = i;
    }
    if(max != 0)
    {
        int t = a[0];
        a[0] = a[max];
        a[max] = t;
    }
    for(int i=0;i<n;i++)
        cout<<a[i]<<" ";
    return 0;
}

1️⃣先定一个整型变量n和一个数组
2️⃣循环输入数组中n个元素的值
3️⃣循环遍历找到数列中的最大值 并进行位置标记
4️⃣将第一个数用整形变量ans存起来,将最大值赋值给第一个元素,再将ans的值赋值给最大值
5️⃣for循环遍历输出

希望对题主有所帮助!可以的话,帮忙点个采纳!

#include<iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int t[n];
    for(int j=0; j<n; j++)
        cin >> t[j];
    int max=t[0],pos=0;
    for(int j=1; j<n; j++)
        if(t[j]>max) {
            max=t[j];
            pos=j;
        }
    int tem=t[0];
    t[0]=t[pos];
    t[pos]=tem;
    for(int j=0; j<n; j++)
        cout << t[j] <<" " ;
    return 0;
}