#include <stdio.h>
int main ()
{
int j,i,temp,min;
int a[9]; //有九个元素的数组
printf("Please input the number for the array:\n");
for(i = 0;i < 9;i++) //读取数组
{
printf("a[%d]= ",i);
scanf("%d",&a[i]);
}
for(i = 0;i < 8; i++) //进行排序
{
for (j = i+1;j < 9;j++) //将第一个元素和后面的元素进行比较,如果前面的比后面的大则将两数交换
{
if (a[i]>a[j]) //进行交换
{
temp = a[j];
a[i] = temp;
a[j] = a[i];
}
}
}
printf("The number in discending order is:"); //以降序进行输出
for (i = 0; i<9; i++)
printf("%4d",a[i]);
return 0;
}
运行结果:
Please input the number for the array:
a[0]= 9
a[1]= 8
a[2]= 7
a[3]= 6
a[4]= 5
a[5]= 4
a[6]= 3
a[7]= 2
a[8]= 1
The number in ascending order is: 1 1 1 1 1 1 1 1 1
--------------------------------
Process exited after 7.233 seconds with return value 0
请按任意键继续. . .
temp = a[j];
a[i] = temp;
a[j] = a[i];
写乱了啊,是:
temp = a[j];
a[j] = a[i];
a[i] = temp;