LeeBlog

导航

HDU 1106 排序

简单排序,这里先把一个字符串按5来分成很多字符串,然后对这些字符串进行比较

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n;
char str1[1005],str2[1005][1005];
void sorts(  )//将字符串分成很多个
{
     memset( str2,0,sizeof( str2 ) );
     int i = 0;
     while( str1[i] )
     {
            if( str1[i]&&str1[i] == '5' )//处理前面多余的5
            {
                ++i;
                continue;
            }
            int j = 0;
            while( str1[i]&&str1[i] == '0' )//处理前面多余的0
                   ++i;
            while( str1[i]&&str1[i] != '5' )//要防止 str[i] == 0;
            {
                   str2[n][j++] = str1[i++];
                   }
            str2[n][j] = 0;
            if( j == 0 )
                str2[n][j] = '0',str2[n][1] = 0;
            ++n;
            }
     return ;
 }
int cmp( char s1[],char s2[] )
{
    int len1 = strlen(s1),len2 = strlen( s2 );
    if( len1 != len2 )
        return len1 - len2;
    else
        return strcmp( s1,s2 );
}
int main( )
{
    while( scanf( "%s",str1 ) != EOF )
    {
           n = 0;
           sorts(  );
           for( int i = 0,f; i < n - 1; ++i )
           {
                f = 0;
                for( int j = 1; j < n; ++j )
                {
                     if( cmp( str2[j-1],str2[j] ) > 0 )//比较两个字符串
                     {
                         f = 1;
                         char ch[1005];
                         strcpy( ch,str2[j-1] );
                         strcpy( str2[j-1],str2[j] );
                         strcpy( str2[j],ch );
                     }
                 }
                 if( !f )
                     break;
           }
           for( int i = 0; i < n; ++i )
                printf( i == 0?"%s":" %s",str2[i] );
           puts( "" );
           }
    return 0;
}

posted on 2011-05-03 19:41  LeeBlog  阅读(480)  评论(0编辑  收藏  举报