c语言,输入n和n个整数,先升序再降序那然后绝对值排序,使用函数的调用
#include<stdio.h>
#include<stdlib.h>
void px1(int *a,int n) //升序
{ int i,j,k; for ( i=0;i<n-1;i++ ) for ( j=i+1;j<n;j++ ) if ( a[i]>a[j] ) { k=a[i]; a[i]=a[j]; a[j]=k; } }
void px2(int *a,int n) //降序
{ int i,j,k; for ( i=0;i<n-1;i++ ) for ( j=i+1;j<n;j++ ) if ( a[i]<a[j] ) { k=a[i]; a[i]=a[j]; a[j]=k; } }
void px3(int *a,int n) //绝对值降序
{ int i,j,k; for ( i=0;i<n-1;i++ ) for ( j=i+1;j<n;j++ ) if ( abs(a[i])<abs(a[j]) ) { k=a[i]; a[i]=a[j]; a[j]=k; } }
void main()
{ int a[256],n,i;
scanf("%d",&n); if ( n<0 ) n=0; else if ( n>256 ) n=256;
for ( i=0;i<n;i++ ) scanf("%d",a+i);
px1(a,n); for ( i=0;i<n;i++ ) printf("%d ",a[i]); printf("\n");
px2(a,n); for ( i=0;i<n;i++ ) printf("%d ",a[i]); printf("\n");
px3(a,n); for ( i=0;i<n;i++ ) printf("%d ",a[i]); printf("\n");
}