C程序找到重复值

时间:2014-08-26 15:32:51

标签: c

我在课堂上得到了我的第一个C程序,我对如何尝试编写它感到迷茫。

以下是提问:

编写C程序以查找:

  • 第一个非重复数字
  • 最后一次非重复数字
  • 第一个重复的数字
  • 最后一个重复的数字

...以一位数字或两位数的非负数字排列。

以下是提供的代码:

#include <stdio.h>

int main()
{
        // Change the elements in the following array
        // to test your program
        int digits[]={2,25,0,26,30,13,25,30,13,0};
        int i;

        printf("In the array of ");
        for (i=0; i<sizeof(digits)/sizeof(int); i++)
                printf("%d ", digits[i]);
        printf("\n");

        // Your code here...

        return 0;
}

我对如何实现这一点略有了解,但我不确定。我想先做两个循环,先重复一次,然后先重复,然后重复一次,最后一次重复,最后重复一次。实现这一目标的任何指导?我是C的新手,对如何正确有效地解决这个问题感到困惑。我不是在寻找直接的答案,而是寻求指导,所以我可以自己做,也可以从中学习。

1 个答案:

答案 0 :(得分:0)

每当你使用字谜,计数和位置检测时,一种非常简单(但可能是内存密集)的方法是使用直方图,这是我最喜欢的统计工具之一。如果digits数组中的最大可能值类似于2000000,则此示例最终可能会占用大量内存,但在这种情况下,它并非如此。

digits中的值用作数组的索引,该数组检测到目前为止是否已计算/检测到该值,其余为高中统计数据。

祝你好运!

PS:我知道这是一个完整的答案,但是如果我们提供不完整的答案,我们也可以将问题标记为&#34;主要基于意见&#34;。

代码清单


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_VALUE (255)          /* Change if needed */
#define MIN_VALUE (0)            /* Leave alone */

typedef struct entry_t {
   bool bUnique;
   int  iNumOccurences;
   int  iFirstOccurrence;
   int  iLastOccurence;
} entry_t;

int main(void) {
   // Change the elements in the following array to test your program
   int i;
   int digits[]={2,25,0,26,30,13,25,30,13,0};
   /* Additional variables needed */
   size_t numDigits = sizeof(digits) / sizeof(int);
   entry_t* histogram;
   bool bFound;

   /* Print out list of digits */
   printf("In the array of ");
   for (i=0; i<numDigits; i++) {
      printf("%d ", digits[i]);
   }
   printf("\n");

   /* Check if limits are obeyed */
   for (i=0; i<numDigits; i++) {
      if ((digits[i] > MAX_VALUE) || (digits[i] < MIN_VALUE)) {
         printf("Element %d at position %d in list exceeds bounds; Exiting\n", digits[i], i);
         return (-1);
      }
   }

   /* Allocate array for statistics; Yes, we're casting malloc, since my
    * compiler doesn't allow this to build without it.
    */
   if ((histogram = (entry_t*)calloc(MAX_VALUE, sizeof(entry_t))) == NULL) {
      printf("Failed to allocate memory\n");
      return (-1);
   }
   for (i=0; i<MAX_VALUE; i++) {
      histogram[i].bUnique = true;
   }
   /* Generate a histogram and statistics */
   for (i=0; i<numDigits; i++) {
      if (histogram[digits[i]].iNumOccurences == 0) {
         /* First detected occurence of this value */
         histogram[digits[i]].iFirstOccurrence = i;
         histogram[digits[i]].iNumOccurences = 1;
      } else {
         /* This is not the first occurrence */
         histogram[digits[i]].bUnique = false;
         histogram[digits[i]].iLastOccurence = i;
         histogram[digits[i]].iNumOccurences++;
      }
   }
   /* Now find the first unique value */
   for (i=0, bFound=false; i<numDigits && !bFound; i++) {
      if (histogram[digits[i]].bUnique) {
         printf("Found first unique value:%d at position %d\n", digits[i], i);
         bFound = true;
      }
   }
   if (!bFound) {
      printf("No unique elements found\n");
   }
   /* Now find the first non-unique value */
   for (i=0, bFound=false; i<numDigits && !bFound; i++) {
      if (!histogram[digits[i]].bUnique) {
         printf("Found first non-unique value:%d at position %d, and the last instance at position %d\n",
               digits[i], i, histogram[digits[i]].iLastOccurence);
         printf("Number of occurrences:%d\n", histogram[digits[i]].iNumOccurences);
         bFound = true;
      }
   }
   if (!bFound) {
      printf("No repeated elements found\n");
   }
   /* Now repeat the above tests, just in reverse */
   for (i=numDigits-1, bFound=false; i>=0 && !bFound; i--) {
      if (histogram[digits[i]].bUnique) {
         printf("Found last unique value:%d at position %d\n", digits[i], i);
         bFound = true;
      }
   }
   if (!bFound) {
      printf("No unique elements found\n");
   }
   for (i=numDigits-1, bFound=false; i>=0 && !bFound; i--) {
      if (!histogram[digits[i]].bUnique) {
         printf("Found last non-unique value:%d at position %d, and the first instance at position %d\n",
               digits[i], i, histogram[digits[i]].iFirstOccurrence);
         printf("Number of occurrences:%d\n", histogram[digits[i]].iNumOccurences);
         bFound = true;
      }
   }
   if (!bFound) {
      printf("No repeated elements found\n");
   }

   /* Cleanup */
   free(histogram);
   histogram = NULL;
   return 0;
}

示例运行


In the array of 2 25 0 26 30 13 25 30 13 0
Found first unique value:2 at position 0
Found first non-unique value:25 at position 1, and the last instance at position 6
Number of occurrences:2
Found last unique value:26 at position 3
Found last non-unique value:0 at position 9, and the first instance at position 2
Number of occurrences:2

<强>参考

  1. 直方图,已访问2014-08-26,<http://en.wikipedia.org/wiki/Histogram>