HDOJ1106 排序

别人的代码【使用sscanf和qsort】

#include<iostream>
using namespace std;

int cmp(const void *a,const void *b){ return *(int*)a-*(int*)b; }
int main()
{
    char s[1001],*p;
    int a[1001],i,l,n,m;
    while (gets(s))
    {
        l=strlen(s);
        p=s;
        for(i=0;i<l;i++) if (s[i]=='5') s[i]=' ';
        l=0;  
        while (sscanf(p,"%d%n",&m,&n)==1) 
        {//注意sscanf()的返回值的使用
            a[l++]=m;
            p+=n;
        }
        qsort(a,l,sizeof(int),cmp);
        printf("%d",a[0]);
   for (i=1;i<l;i++) printf(" %d",a[i]); 
        printf("\n"); 
    }
    return 0;
}
 

 别人的代码【使用strtok和qsort】

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
int cmp(const void* a,const void* b){ return *(int*)a-*(int*)b;}
int main()
{
char a[1000];
int b[1000],size,i;
char* p;
while(EOF!=scanf("%s",a))
{
   size=0;
   p = strtok(a,"5");
   while(NULL != p)
   {
    b[size++] = atoi(p);
    p = strtok(NULL,"5");
   }
   qsort(b,size,sizeof(int),cmp);
   for(i=0;i<size-1;i++)
    printf("%d ",b[i]);
   printf("%d\n",b[i]);
}
return 0;
}

 

posted @ 2012-04-13 09:05  ZH奶酪  阅读(442)  评论(0编辑  收藏  举报