2014-07-07 34 views
1

这是一个任务,所以我不希望直接的答案;相反,任何逻辑帮助我的算法(或指出任何逻辑缺陷)将非常有帮助和赞赏!如何使用C++计算1-100数组中覆盖率的百分比?

我有一个程序从用户接收“n”个元素放入一维数组中。 该阵列使用随机生成的数字。 IE:如果用户输入88,则生成88个随机数(每个在1到100之间)的列表)。 “n”最大为100.

我必须写2个函数。

功能#1:

Determine the percentage of numbers that appear in the array of "n" elements. 
So any duplicates would decrease the percentage. 
And any missing numbers would decrease the percentage. 
Thus if n = 75, then you have a maximum possible %age of 0.75 
(this max %age decreases if there are duplicates) 

This function basically calls upon function #2. 

FUNCTION HEADER(GIVEN) = "double coverage (int array[], int n)" 

功能#2:

Using a linear search, search for the key (key being the current # in the list of 1 to 100, which should be from the loop in function #1), in the array. 

Return the position if that key is found in the array 
(IE: if this is the loops 40th run, it will be at the variable "39", 
and will go through every instance of an element in the array 
and if any element is equal to 39, all of those positions will be returned? 
I believe that is what our prof is asking) 

Return -1 if the key is not found. 

Given notes = "Only function #1 calls function #2, 
and does so to find out if a certain value (key) is found among the first n elements of the array." 

FUNCTION HEADER(GIVEN) = "int search (int array[], int n, int key)" 

我真正需要帮助是算法的逻辑。

我将不胜感激任何帮助,因为我会以完全不同于我们的教授想要的方式来解决这个问题。

我的第一个想法是在1到100的所有变量键中遍历函数#1。 然后在该循​​环中转到搜索函数(函数#2),其中循环会遍历每个数字在数组中,并且如果数字是(1)数组中存在重复或(2)不存在,则将其添加到计数器中。然后,我会从100中减去该计数器。因此,如果除了#40和#41之外所有数字都包含在数组中,然后#77是重复的,则覆盖百分比将为100 - 3 = 97%。

虽然当我输入这个,我认为这本身可能有缺陷? ^因为在数组中最多有100个元素,如果唯一缺少的数字是99,那么您将减去1以使该数字缺失,然后如果有重复,则会减去另一个1,因此您的覆盖百分比将(100-2)= 98,当它应该是99.

而这正是我真的很感谢任何逻辑帮助。 :)

我知道我有一些问题接近这个逻辑。

我想我可以找出相对容易的编码;我最挣扎的是要采取的步骤。所以任何伪代码想法都会很棒!

(我可以张贴我的整个程序代码,到目前为止,如果有必要的人,就问,但它是相当长到现在为止,我已经在计划执行其他任务的许多其他功能) 编辑:我看到多人编辑我的帖子/标签。标签不是我放的xD所以请不要低估我的问题,因为您必须编辑它?D:有人只是将它编辑回原来的方式,在别人编辑它之前。啊

+0

感谢Theolodis修复我的blockquotes。:)不知道我是如何切换到只能输入“>”来打CMD + K的。哈哈。 – Kaitlyn

+1

“通过函数#1循环”没有任何意义。函数#1计算最终答案,所以你只需要调用一次。但是,函数#1的描述表明您应该在函数#1的实现中调用函数#2来计算结果。 –

+0

根据我的教授笔记(他给我们函数标题),我的理解是我们必须使用函数#1循环遍历1到100的数字。并使用搜索函数#2比较当前数字1 - 100到数组中的所有数字。 //给函数#2的函数头是“int search(int array [],int n,int key)”。函数#1的头文件是“double coverage(int array [],int n)” – Kaitlyn

回答

1

我可能是错的,但因为我读了所有你需要做的是:

  • 编写过n个元素的数组循环,以找到它给定数的函数。它将返回第一次出现的索引,或者在数组无法找到的情况下返回负值。
  • 编写一个循环来调用所有数字1到100的函数并对查找进行计数。然后将结果除以100.
0

我不确定我是否理解这个事情是正确的,但是1个函数可以做到这一点,如果你不关心速度,最好把数组放入vector,通过1..100循环并使用增强查找功能http://www.boost.org/doc/libs/1_41_0/doc/html/boost/algorithm/find_nth.html。在那里,您可以将当前值与向量中的第二个输入值进行比较,如果它包含减少而不是减少,如果要查找唯一编号是否在数组中,请使用http://www.cplusplus.com/reference/algorithm/find/。我不明白,百分比如何下降,所以它是在你自己的,我不明白第二个功能,但如果它的线性搜索使用再次发现。 P.S.矢量描述http://www.cplusplus.com/reference/vector/vector/begin/

+0

感谢您的回复!不幸的是,我必须遵守100%的指导方针。哈哈。我知道,在可能有更简单的方法做某事时,必须严格遵守某些事情是令人沮丧的。:( 我们在课堂上还没有涉及到矢量,因此无法使用它们,如果我这样做,我会失分。 – Kaitlyn

0

您想知道给定数组中出现[1,100]范围内的数字。可以依次检索每个号码:

size_t count_unique(int array[], size_t n) 
{ 
    size_t result = 0; 

    for (int i = 1; i <= 100; ++i) 
    { 
     if (contains(array, n, i)) 
     { 
      ++result; 
     } 
    } 

    return result; 
} 

所有你仍然需要的是包容检查contains(array, n, i)的执行情况,并改造独特的计数成比例(通过划分)。