回复 21楼 lin5161678
那我改改看~
程序代码:
#include<stdio.h>
int comp( const void*,const void* );
unsigned fun( unsigned,unsigned );
unsigned print( unsigned const char* );
int main( void )
{
unsigned a;
unsigned b;
if (scanf("%u%u",&a,&b)!=2)
return 1;
printf("%u\n",fun(a,b));
return 0;
}
int comp( const void* p,const void* q )
{
return *( unsigned char* )p-*( unsigned char* )q;
}
#include<stdlib.h>
#include<string.h>
#include<assert.h>
unsigned fun( unsigned a,unsigned b )
{
unsigned count;
unsigned char* p=( unsigned char* )calloc(++a,b);
assert(p);
memset(p,255,b);
qsort(p,a,b,comp);
count=print(p);
free(p);
return count;
}
unsigned print( unsigned const char* p )
{
unsigned i=0;
while (*p++==0)
++i;
return i;
}
就是把char改成unsigned char,嗯,这样就可以了,的确有些编译环境会把char自动默认成unsigned char,也就是char里面没有出现负数~
其实简单点的确不用考虑unsigned的问题,改两个地方就可以了
~
程序代码:
#include<stdio.h>
int comp( const void*,const void* );
unsigned fun( unsigned,unsigned );
unsigned print( const char* );
int main( void )
{
unsigned a;
unsigned b;
if (scanf("%u%u",&a,&b)!=2)
return 1;
printf("%u\n",fun(a,b));
return 0;
}
int comp( const void* p,const void* q )
{
return *( char* )p-*( char* )q;
}
#include<stdlib.h>
#include<string.h>
#include<assert.h>
unsigned fun( unsigned a,unsigned b )
{
unsigned count;
char* p=( char* )calloc(++a,b);
assert(p);
memset(p,1,b);
qsort(p,( unsigned )(a,b,comp);
count=print(p);
free(p);
return count;
}
unsigned print( const char* p )
{
unsigned i=0;
while (*p++==0)
++i;
return i;
}
[此贴子已经被作者于2018-6-7 11:31编辑过]