-1

这个函数接受一个整数数组,数组中元素的数量,并尝试在数组中找到一个多数元素。如果多数元素存在,则将其放在 *result 中并且函数返回 true。如果不存在多数元素,则函数返回 false。在这种情况下,不应使用 *result。

我的输出对于我正在编写的程序不能正常工作,这是因为我认为这个 findMajority 函数。

这就是输出的样子: http: //pastebin.com/Q5ycXHrg

这就是我的输出:http: //pastebin.com/7P1ZTpML

这是输入:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1
1 2 3
1 1 1
1 2 1
1 2
1 1
2
1 1 1 1 2 3 4 5 6 7

这是功能:

int findMajority(int *array, int count, int *result){ 
  int i, counter, bcount = 0, ccount = 0, candidate, j;    

  if(count == 1) {
      *result = *array;
      return true;
  }

  if(count % 2 != 0 ) {
    for(i = 0; i < count; i++) {
      if(*(array + i) == *(array + count)) {
        counter++;
      }
    }

    if(counter > (count/2)) {
      *result = *(array + count);
      return true;
    }
    else {
      *(array + count) = 0;
      count--;
    }
  }

  for(j=0; j <= count; j += 2) {
    if(*(array + j) == *(array + (j + 1))) {
      *(array + (count + 1)) = *(array + j);
      bcount++;//how many numbers on the end of the array
    }
  }

  if(bcount == 1) {
    int k = count;
    while(*(array + k) == 0) {
      candidate = *(array + k);
    }
  }
  else
    findMajority((array + count), count, result);

  for(j=0; j <= count; j += 2) {
    if(*(array + j) == candidate) {
      ccount++;
    }
  }

  if(ccount > (count/2)) {
    *result = candidate;
    return true;
  }
  else
    return false;  
}
4

2 回答 2

1

I suggest you learn how to use a debugger to debug your program. For example, after wrapping your code with the following:

#include <stdio.h>

typedef enum { false, true } boolean;

 // ((( your function here )))

int main(int argc, char* argv[])
{
   int result = 0;
   int number = 0;

   int test[] = { 1, 1, 1, 1, 1, 1, 1 };


   result = findMajority(&test[0], sizeof(test) / sizeof(int), &number);

   printf("Result = %d, Number = %d\n", result, number);

   return 0;
}

Assuming you put this into 'question.c' you could then issue the commands (assuming you have gcc and gdb):

$ gcc -g -o question question.c
$ gdb ./question
(gdb) b findMajority
Breakpoint 1 at 0x80483ea: file question.c, line 6.
(gdb) run
Starting program: ./question
Breakpoint 1, findMajority (array=0xbffff4bc, count=7, result=0xbffff4d8) at question.c:6
6     int i, counter, bcount = 0, ccount = 0, candidate, j;    

You can then use the n command to step to the next line, and the p command to print variables to see what's going wrong. For example, you could find some of the problems that Toms pointer out relatively quickly:

39     while(*(array + k) == 0){
(gdb) n
40         candidate = *(array + k);
(gdb) n
39     while(*(array + k) == 0){
(gdb) n
40         candidate = *(array + k);
(gdb) n
39     while(*(array + k) == 0){
(gdb) n

There's your infinite loop.

(gdb) p counter
$3 = -1207959944

And there's your uninitialized counter.

Part of learning programming is figuring out strategies of determining just what went wrong. Some people like to use text-based debuggers like gdb. Some people like graphical debuggers like you could find in Eclipse CDT. Some people put printf() statements throughout their code.

Once you're really good, like Toms, you can just read it and rattle off the problems. ;-)

于 2013-03-02T02:02:42.870 回答
1

你的功能有很多问题。

  1. 没有初始化counter你正在增加它
  2. 检查是否array[count]是有效的最后一个元素或者array[count-1]是正确的
  3. 在这段代码中 for(j=0; j <= count; j += 2){

       if(*(array + j) == *(array + (j + 1))){
           *(array + (count + 1)) = *(array + j);
           bcount++;//how many numbers on the end of the array
       }}
    

    因为count= 3您正在访问array[4] array[5]等。

  4. 这是一个无限循环。您没有在循环内修改条件变量 while(*(array + k) == 0) { candidate = *(array + k); }
于 2013-03-02T01:46:38.823 回答