View Code of Problem 49

#include <stdio.h>
//有一个长度为n的整数序列。请写一个程序,把序列中的最小值与第一个数交换,最大值与最后一个数交换。输出转换好的序列。
int main()
{
  int n,i,temp;
  int max = 0,min = 0;
  scanf("%d",&n);
  int a[n];
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
    if(a[i]>a[max])
      max = i;	
    if(a[i]<a[min])
      min = i;    
  }
  temp = a[max];
  a[max] = a[n-1];
  a[n-1] = temp;
	
  temp = a[min];
  a[min] = a[0];
  a[0] = temp;    
  
  for(i=0;i<n-1;++i)
    printf("%d ",a[i]);
  printf("%d",a[n-1]);
  return 0;  
}

Double click to view unformatted code.


Back to problem 49