HDoj 2016 数据的交换输出

Problem Description
输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。
 

 

Input
输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。
 

 

Output
对于每组输入数据,输出交换后的数列,每组输出占一行。
 

 

Sample Input
4 2 1 3 4 5 5 4 3 2 1 0
 

 

Sample Output
1 2 3 4 1 4 3 2 5
 

 

Author
lcy
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2017 2018 2024 2019 2020 
 
 
和HDoj2015,2010一样,每行末尾要注意空格,不然oj会出现格式错误
 
C语言代码如下:
#include<stdio.h>
#include<malloc.h>
int main()
{
    int n=0;
    int min;
    int pod;
    int temp;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        int *a=(int *)malloc(sizeof(int)*n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        min=a[0];
        pod=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]<min)
            {
                min=a[i];
                pod=i;
            }
        }
        temp=a[0];
        a[0]=a[pod];
        a[pod]=temp;
        printf("%d",a[0]);
        for(int i=1;i<n;i++)
            printf(" %d",a[i]);            //将第一个元素与之后的分开输出,防止一行末尾出现多余的空格 
        printf("\n");    
    }
    return 0;
}

 

posted on 2020-03-18 11:44  沈香茶  阅读(97)  评论(0)    收藏  举报