交换最小值和最大值

| October 30, 2007 13:54 | timmy | Via Original
输入一个正整数repeat (0输入一个正整数n (1例:括号内是说明
输入
3   (repeat=3)
5 4 3 5 1 2
4 1 5 6 7
5 5 4 3 2 1
输出
1 3 2 4 5
1 5 6 7
1 4 3 2 5


#include <stdio.h>
int main( )
{
  int ri, repeat;
  int i, index, n, t;
  int a[10];

  scanf("%d", &repeat);
  for(ri=1; ri<=repeat; ri++){
     scanf("%d", &n);
     for(i=0; i<n; i++)
        scanf("%d", &a[i]);
    index=0;
    for(i=1; i<n; i++)
      if(a[i]<a[index])
        index=i;
    t=a[index];
    a[index]=a[0];
    a[0]=t;
    for(i=1; i<n; i++)
      if(a[i]>a[index])
        index=i;
    t=a[index];
    a[index]=a[n-1];
    a[n-1]=t;
    for(i=0; i<n; i++)
        printf("%d ", a[i]);
     printf("\n");
  }
}
Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(310)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember