C语言实验——最值

来源:互联网 发布:淘宝发布的宝贝不见了 编辑:程序博客网 时间:2024/06/06 11:02

C语言实验——最值

Time Limit: 1000MS Memory Limit: 65536KB

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

Hint

Author

#include<stdio.h>#include<stdlib.h>int main(){    int n,i,max = 0,min = 0,x,y;    scanf("%d",&n);    int a[n+5];//防止数组越界;    scanf("%d",&a[0]);    max = a[0];    min = a[0];    for(i = 1;i < n;i++)    {        scanf("%d",&a[i]);        if(max < a[i])        {            x = i;//用x记录最大值的下标,便于交换位置;            max = a[i];        }        if(min > a[i])        {            y = i;//同理,y记录最小值的下标;            min = a[i];        }    }    //交换位置;    i = a[0];    a[0] = a[y];    a[y] = i;    i = a[n-1];    a[n-1] = a[x];    a[x] = i;    for(i = 0;i < n;i++)    {        if(i==0)printf("%d",a[i]);        else printf(" %d",a[i]);    }    printf("\n");    return 0;}
原创粉丝点击