HDU1004之总是wa的细节问题

 1 #include <stdio.h>

 2 #include <string.h>

 3 int main()

 4 {

 5 

 6         char color[1000][16];

 7         int n, i, k, j, max, max_i;

 8         while( scanf("%d", &n) != EOF && n != 0 ){

 9     //    while( n-- ){ #1

10               /*输入*/

11               for( i = 0; i < n; i ++ )

12               {

13 

14                     getchar();

15                     scanf("%s", color[i]);

16 

17               }

18                  if( n == 1 )

19                     printf("%s\n", color[0]);

20               else

21               {

22                     max = 1;

23                     /*搜索比较*/

24                     for( i = 0; i < n - 1; i ++ )

25                     {

26                         

27                             k = 1;

28                             for( j = i + 1; j < n; j ++ )

29                             {

30 

31                                 //    if( color[i] == color[j] )#2

32                                     if( strcmp( color[i], color[j] ) == 0 )

33                                             k ++;

34                                 

35                             }

36                             if( k > max )

37                             {

38                                     max = k;

39                                     max_i = i;

40                                     

41                             }

42 

43                     }

44                     /*输出*/

45                     printf("%s\n", color[max_i]);

46 

47               }

48 

49         }

50         return 0;

51 

52 }
View Code

 

 

 note:

 #1: 为什么不用while( n-- )? /* 当n值为1时, 执行n-- 之后,它的值为0,这样会跳过下面的输入,导致无法正常输出color[0].*/

  #2: 常规错误:两个字符串不能用 == 比较

你可能感兴趣的:(HDU)