0%

输入的数组中最大元素与首元素交换,最小的与末元素交换,并输出

文章时效性提示

本文发布于 496 天前,部分信息可能已经改变,请注意甄别。

题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
void Exchange_Biggest(int* input)
{
    int Biggest = input[0];
    int i;
    int temp;
    int record_Biggest = 0;
    for (i=0; i<10; i++)
    {
        if(input[i]>Biggest)//遍历数组,如果比记录到最大的还大,就把Biggest的值换成更大的。
        {
            Biggest = input[i];
            record_Biggest = i;
        }
    }
    temp = input[0];//把第一个值和最大的交换一下
    input[0] = input[record_Biggest];
    input[record_Biggest] = temp;
}
void Exchange_Smallest(int* input)
{
    int Smallest = input[0];
    int i;
    int temp;
    int record_Smallest = 0;
    for (i=0; i<10; i++)
    {
        if(input[i]<Smallest)
        {
            Smallest = input[i];
            record_Smallest = i;
        }
    }
    temp = input[9];
    input[9] = input[record_Smallest];
    input[record_Smallest] = temp;
}
int main() {
    int input[10];
    int i;
    for (i=0; i<10; i++)//输入数组
    {
        scanf("%d",&input[i]);
    }
    Exchange_Biggest(input);//交换最大的数
    Exchange_Smallest(input);//交换最小的数
    for (i=0; i<10; i++) {
        printf("%d ",input[i]);
    }
    return 0;
}