C语言实验——最值

来源:互联网 发布:java int 乘法 编辑:程序博客网 时间:2024/06/02 05:08

Submit Statistic
Problem Description
有一个长度为n的整数序列,其中最大值和最小值不会出现在序列的第一和最后一个位置。
请写一个程序,把序列中的最小值与第一个数交换,最大值与最后一个数交换。输出转换好的序列。
Input
输入包括两行。
第一行为正整数n(1≤n≤10)。
第二行为n个正整数组成的序列。
Output
输出转换好的序列。数据之间用空格隔开。
Example Input
5
2 1 5 4 3
Example Output
1 2 3 4 5

#include <iostream>#include <math.h>#include <cstdio>using namespace std;int a[20];int main(){    int n;    cin>>n;    for(int i = 0; i < n; i++)    {        cin>>a[i];    }    int min, max;    min = a[0];    max = a[n-1];    int t, l;    t = 0;    l = n-1;    for(int i = 0 ;i < n; i++)    {        if(min > a[i])            {                min = a[i];                t = i;            }        if(max < a[i])        {            l = i;            max = a[i];        }    }    swap(a[0], a[t]);    swap(a[n-1], a[l]);    for(int i = 0; i < n; i++)    {        if(i == 0)            cout<<a[0];        else            cout<<" "<<a[i];    }    cout<<endl;    return 0;}
原创粉丝点击