自由尋覓快樂別人從沒法感受

0%

C语言学习第五章T5

题目:查找,替换数字并输出下标。

输入5个整数,并存储在数组中,找出最大数与最小数所在的下标位置,并把两者对调,然后输出调整后的5个数。

主要代码:

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
52
# include <stdio.h>
int main (void)
{
int a[5],max,min,i,x,y,t;

//input:
printf("input 5 numbers:");
for (i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("start:");
for (i=0;i<5;i++)
{
printf("%4d",a[i]);
}
printf("\n");

//judge:
max = a[0];
min = a[0];
x = y = 1;
for (i=1;i<5;i++)
{
if (a[i]>max)
{
max = a[i];
x = i+1;
}
if (a[i]<min)
{
min = a[i];
y = i+1;
}
}

//exchange:
t = a[x-1];
a[x-1] = a[y-1];
a[y-1] = t;

//output:
printf("max is the %d number,max=%d\n",x,max);
printf("min is the %d number,min=%d\n",y,min);
printf("after exchange:");
for (i=0;i<5;i++)
{
printf("%4d",a[i]);
}
printf("\n");
return 0;
}

运行结果: