HDU 1004 Let the Balloon Rise我的程序为何会WA啊求助!
#include<stdio.h>#include<string.h>chars[1010][16],maxs[16];intcmp(constvoid*p,constv...
#include<stdio.h>
#include<string.h>
char s[1010][16], maxs[16];
int cmp( const void *p, const void *q )
{
return strcmp( ( char * )p, ( char * )q );
}
int main()
{
int i, n = 1, k, maxest, samen;
while ( 1 == 1 )
{
memset( maxs, 48, sizeof( maxs ) );
maxest = 0;
scanf( "%d", &n );
k = 1;
if ( n == 0 )
break;
for ( i = 1; i <= n; i++ )
scanf( "%s", s[i] );
qsort( s[1], n, sizeof( s[0] ), cmp );
while( k < n )
{
k++;
if ( strcmp( s[k], s[k - 1] ) == 0 )
{
samen = 1;
k--;
while( k < n )
{
k++;
if ( strcmp( s[k], s[k - 1] ) == 0 )
samen += 1;
else if ( samen > maxest )
{
sprintf( maxs, "%s", s[k - 1] );
maxest = samen;
k--;
break;
}
else
{
k--;
break;
}
}
if ( samen > maxest )
{
sprintf( maxs, "%s", s[k] );
maxest = samen;
}
}
}
printf( "%s\n", maxs );
}
return 0;
}
我是把读入的所有数据排序了 再输出次数最多的颜色! 展开
#include<string.h>
char s[1010][16], maxs[16];
int cmp( const void *p, const void *q )
{
return strcmp( ( char * )p, ( char * )q );
}
int main()
{
int i, n = 1, k, maxest, samen;
while ( 1 == 1 )
{
memset( maxs, 48, sizeof( maxs ) );
maxest = 0;
scanf( "%d", &n );
k = 1;
if ( n == 0 )
break;
for ( i = 1; i <= n; i++ )
scanf( "%s", s[i] );
qsort( s[1], n, sizeof( s[0] ), cmp );
while( k < n )
{
k++;
if ( strcmp( s[k], s[k - 1] ) == 0 )
{
samen = 1;
k--;
while( k < n )
{
k++;
if ( strcmp( s[k], s[k - 1] ) == 0 )
samen += 1;
else if ( samen > maxest )
{
sprintf( maxs, "%s", s[k - 1] );
maxest = samen;
k--;
break;
}
else
{
k--;
break;
}
}
if ( samen > maxest )
{
sprintf( maxs, "%s", s[k] );
maxest = samen;
}
}
}
printf( "%s\n", maxs );
}
return 0;
}
我是把读入的所有数据排序了 再输出次数最多的颜色! 展开
1个回答
展开全部
很明显的,你的程序如果是 n = 1的时候进不去循环 while(k < n) 的循环,所以答案肯定错了啊。
可以对n=1的情况进行特判:
#include<stdio.h>
#include<string.h>
#include <algorithm>
char s[1010][16], maxs[16];
int cmp( const void *p, const void *q )
{
return strcmp( ( char * )p, ( char * )q );
}
int main()
{
int i, n = 1, k, maxest, samen;
while ( 1 == 1 )
{
memset( maxs, 48, sizeof( maxs ) );
maxest = 0;
scanf( "%d", &n );
k = 1;
if ( n == 0 )
break;
for ( i = 1; i <= n; i++ )
scanf( "%s", s[i] );
qsort( s[1], n, sizeof( s[0] ), cmp );
while( k < n )
{
k++;
if ( strcmp( s[k], s[k - 1] ) == 0 )
简渣余 {
samen = 1;
k--;
while( k < n )
{
k++;
if ( strcmp( s[k], s[k - 1] ) == 0 )
samen += 1;
else if ( samen > maxest )
{
梁厅 sprintf( maxs, "%s", s[k - 1] );
maxest = samen;
k--;
break;
}
else
{
k--;
break;
}
}
if ( samen > 拦滚maxest )
{
sprintf( maxs, "%s", s[k] );
maxest = samen;
}
}
}
if(n != 1)
printf( "%s\n", maxs );
else
printf( "%s\n", s[1] );
}
return 0;
}
更多追问追答
追问
我是谁
我试试
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询