这是一个任务,所以我不希望直接的答案;相反,任何逻辑帮助我的算法(或指出任何逻辑缺陷)将非常有帮助和赞赏!如何使用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:有人只是将它编辑回原来的方式,在别人编辑它之前。啊
感谢Theolodis修复我的blockquotes。:)不知道我是如何切换到只能输入“>”来打CMD + K的。哈哈。 – Kaitlyn
“通过函数#1循环”没有任何意义。函数#1计算最终答案,所以你只需要调用一次。但是,函数#1的描述表明您应该在函数#1的实现中调用函数#2来计算结果。 –
根据我的教授笔记(他给我们函数标题),我的理解是我们必须使用函数#1循环遍历1到100的数字。并使用搜索函数#2比较当前数字1 - 100到数组中的所有数字。 //给函数#2的函数头是“int search(int array [],int n,int key)”。函数#1的头文件是“double coverage(int array [],int n)” – Kaitlyn