Hdu1106排序

来源:互联网 发布:excel同一列重复数据 编辑:程序博客网 时间:2024/05/22 02:20

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1106

此题略坑错了好几次  注意开头和结尾多个5的情况

提供几组数据

500

0 0

512351245

123 124

55123

123

5512350

0 123

贴上代码 排序我用的是快排 没有用冒泡 不过冒泡应该也可以毕竟数据不多

#include <stdio.h>
#include <string.h>
void quickSort(int a[],int left,int right)
{
    int i,j,temp;
    i=left;
    j=right;
    temp=a[i];
    if(left>=right)
        return ;
    while(i!=j)
    {
        while(i<j&&a[j]>=temp)
            j--;
        if(i<j)
            a[i]=a[j];
        while(i<j&&a[i]<=temp)
            i++;
        if(i<j)
            a[j]=a[i];
    }
    a[i]=temp;
    quickSort(a,left,i-1);
    quickSort(a,i+1,right);
}
int main()
{
    char ch[10000];
    int a[10000],i;
    while(scanf("%s",ch)!=EOF)
    {
        memset(a,0,sizeof(a));
        int len=strlen(ch);
        int k=0;
        for(i=0; i<len; i++)
        {
            if((ch[i]-'0')==5)
                continue;
            while((ch[i]-'0')!=5&&i<len)
            {
                a[k]=a[k]*10+(ch[i]-'0');
                i++;
            }
            k++;
        }
        quickSort(a,0,k-1);
        for(i=0; i<k; i++)
        {
            if(i==0)
                printf("%d",a[i]);
            else
                printf(" %d",a[i]);
        }
        printf("\n");
    }
    return 0;
}

原创粉丝点击