:)后来我发现原来我原来的思路是错的。重写了一断代码,不过,如果不是在特殊的机器上,效率很低:在比较大小的时候用了2次乘法。
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#undef NDEBUG
#include <assert.h>
#define N 10000
int
cmpFun (long long n1,long long n2)
{ /* 比较大小 */
static const long long table [ ] = {
1LL,10LL,100LL,1000LL,10000LL,100000LL,1000000LL,10000000LL,100000000LL,1000000000LL,10000000000LL,
100000000000LL,1000000000000LL,10000000000000LL,100000000000000LL,100000000000000LL,1000000000000000LL,10000000000000000LL};
char *p1 =( char[20] ){ 0 } ,
*p2 =( char[20] ){ 0 } ;
sprintf( p1,"%I64d",n1 ); assert( strlen(p1)<19 );
sprintf( p2,"%I64d",n2 ); assert( strlen(p1)<19 );
return n1 * table[ strlen(p2) ] + n2 > n2*table[ strlen(p1) ] +n1 ? 1 : -1 ;
}
void
sort (long long *arrNum,unsigned long j)
{ /* 冒泡法排序 */
#define SWAP(x,y,temp) ( (temp)=(x), (x)=(y), (y)=(temp) )
long long
temp =0, *walk,
*first = arrNum,
*last = arrNum+j+1;
for ( ; first<--last; )
for ( walk=first-1; ++walk<last; )
if ( cmpFun(walk[0],walk[1]) < 0 ) SWAP(walk[0],walk[1],temp);
}
int
main( void )
{
int i, j, ch , nZero=0 ,n;
long long input,arrNum[N]={0};
cmpFun( 0,0 );
scanf( "%d",&n ); assert( n<N );
for ( j=i=-1; ++i < n ; )
{
scanf( "%I64d",&input ); //输入long long(64位) 数
if ( input<0LL ) break; //输入了负数,退出输入
else if ( input==0LL ) ++nZero; //输入的是0,不要参加排序,最后直接输出就好了
else arrNum[++j]=input; //记录数据,等下好参加排序
while( (ch=getchar()) != '\n' ) ; //吸收非法字符
}
sort(arrNum,j);
puts ( "\n--------------------------------");
for ( i=-1; ++i<=j ; ) //输出结果
printf( "%I64d",arrNum[i] );
if ( nZero ) printf( "%0*d",nZero,0); //输出 0
//system( "pause" );
return 0;
}
[
本帖最后由 Windy0Winll 于 2010-9-5 13:23 编辑 ]