简体   繁体   English

无法弄清楚在给定情况下如何在输出中编程

[英]Can't figure out how to program in outputs for the given situations

So, I'm working on an assignment for my intro to computer science class. 因此,我正在为我的计算机科学入门课程做作业。 The assignment is as follows. 分配如下。

There is an organism whose population can be determined according to the following rules: 有一个生物,其种群可以根据以下规则确定:

The organism requires at least one other organism to propagate. 该生物需要至少一种其他生物进行繁殖。 Thus, if the population goes to 1, then the organism will become extinct in one time cycle (eg one breeding season). 因此,如果种群数量达到1,则该生物将在一个时间周期内(例如一个繁殖季节)灭绝。 In an unusual turn of events, an even number of organisms is not a good thing. 在不同寻常的事件中,数量均匀的生物不是一件好事。 The organisms will form pairs and from each pair, only one organism will survive If there are an odd number of organisms and this number is greater than 1 (eg, 3,5,7,9,…), then this is good for population growth. 这些生物将成对形成,并且每对中只有一个生物可以生存。如果存在奇数个生物并且该数目大于1(例如3、5、7、9等),那么这对种群有利生长。 The organisms cannot pair up and in one time cycle, each organism will produce 2 other organisms. 这些生物无法配对,并且在一个时间周期内,每个生物都会产生另外两个生物。 In addition, one other organism will be created. 另外,将产生另一种生物。 (As an example, let us say there are 3 organisms. Since 3 is an odd number greater than 1, in one time cycle, each of the 3 organisms will produce 2 others. This yields 6 additional organisms. Furthermore, there is one more organism produced so the total will be 10 organisms, 3 originals, 6 produced by the 3, and then 1 more.) (例如,假设有3个生物。由于3个是大于1的奇数,因此在一个时间周期内,这3个生物中的每一个都会产生另外2个生物。这又产生了6个生物。此外,还有一个生物产生的生物,因此总共将是10个生物,3个原始生物,3个产生的6种生物,然后再增加1个。)

A: Write a program that tests initial populations from 1 to 100,000. 答:编写一个程序来测试从1到100,000的初始人口。 Find all populations that do not eventually become extinct. 找到所有最终不会灭绝的种群。

Write your answer here: 在这里写下您的答案:

B: Find the value of the initial population that eventually goes extinct but that has the largest number of time cycles before it does. B:找到最终灭绝的初始种群的值,但是在此之前具有最大数量的时间周期。

Write your answer here: 在这里写下您的答案:

The general idea of what I have so far is (lacking sytanx) is this with P representing the population 到目前为止,我的总体想法是(缺少Sytanx),其中P代表总体

int generations = 0; int generations = 0;

{ {

if (P is odd) //I'll use a modulus modifier to divide by two and if the result is not 0 then I'll know it's odd 如果(P是奇数)//我将使用一个模数修饰符除以2,如果结果不为0,那么我会知道它是奇数

P = 3P + 1 P = 3P + 1

else 其他

P = 1/2 P P = 1/2 P

generations = generations + 1 世代=世代+ 1

} }

The problem for me is that I'm uncertain how to tell what numbers will not go extinct or how to figure out which population takes the longest time to go extinct. 对我来说,问题是我不确定如何确定哪些数字不会灭绝,或者如何确定哪些人口需要最长时间灭绝。 Any suggestions would be helpful. 任何的意见都将会有帮助。

Basically what you want to do is this: wrap your code into a while-loop that exits if either P==1 or generations > someMaxValue. 基本上,您要做的是:将代码包装到while循环中,如果P == 1或generations> someMaxValue退出,则退出。

Wrap this construct into a for-loop that counts from 1 to 100,000 and uses this count to set the initial P. 将此构造包装为一个从1到100,000的for循环,并使用此计数设置初始P。

If you always store the generations after your while-loop (eg into an array) you can then search for the greatest element in the array. 如果您总是将几代存储在while循环之后(例如,存储到数组中),则可以搜索数组中最大的元素。

This problem can actually be harder than it looks at the first sight. 实际上,这个问题可能比乍一看要难。 First, you should use memorization to speed things up - for example, with 3 you get 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 -> 0, so you know the answer for all those numbers as well (note that every power of 2 will extinct). 首先,您应该使用记忆来加快速度-例如,使用3可以得到3-> 10-> 5-> 16-> 8-> 4-> 2-> 1-> 0,因此您知道答案所有这些数字也是如此(请注意,每2的幂将消失)。

But as pointed out by @Jerry, the problem is with the generations which eventually do not extinct - it will be difficult to say when to actually stop. 但正如@Jerry所指出的那样,问题出在最终没有灭绝的几代人中-很难说何时真正停止。 The only chance is that there will (always) be a recurrence (number of organisms you already passed once when examining the current number of organisms), then you can say for sure that the organisms will not extinct. 唯一的机会是(总是)会复发(检查当前生物数量时已经通过一次的生物数量),那么您可以肯定地说这些生物不会灭绝。

Edit: I hacked a solution quickly and if it is correct, you are lucky - every population between 1-100,000 seems to eventually extinct (as my program terminated so I didn't actually need to check for recurrences). 编辑:我很快找到了一个解决方案,如果它是正确的,那么您很幸运-人口在1到100,000之间的人们似乎最终都灭绝了(由于我的程序终止了,所以我实际上不需要检查重复性)。 Will not give you the solution for now so that you can try by yourself and learn, but according to my program the largest number of cycles is 351 (and the number is close to 3/4 of the range). 现在不会给您解决方案,以便您可以自己尝试并学习,但是根据我的计划,最大的循环数是351(并且该数接近该范围的3/4)。 According to the google search for Collatz conjecture, that is a correct number (they say 350 to go to population of 1, where I'm adding one extra cycle to 0), also the initial population number agrees. 根据谷歌对Collat​​z猜想的搜索,这是一个正确的数字(他们说350表示1的人口,在这里我将一个额外的周期加到0),也符合初始人口数。

One additional hint: Check for integer overflow, and use 64-bit integer (unsigned __int64, unsigned long long) to calculate the population growth, as with 32-bit unsignet int, there is already an overflow in the range of 1-100,000 (the population can indeed grow much higher intermediately) - that was a problem in my initial solution, although it did not change the result. 另一个提示:检查整数溢出,并使用64位整数 (无符号__int64,无符号long long)来计算总体增长,与32位unsignet int一样,已经存在1-100,000的溢出(人口的中间部分确实可以增长很多)-尽管这并没有改变结果,但这是我最初的解决方案中的问题。 With 64-bit ints I was able to calculate up to 100,000,000 in relatively decent time (didn't try more; optimized release MSVC build), for that I had to limit the memo table to first 80,000,000 items to not go out of memory (compiled in 32-bit with LARGEADDRESSAWARE to be able to use up to 4 GB of memory - when compiled 64-bit the table could of course be larger). 使用64位整数,我能够在相对不错的时间内计算出多达1亿个内容(尝试不多;优化版本的MSVC构建),因为我不得不将备忘录表限制为前80,000,000个项目,以免耗尽内存(使用LARGEADDRESSAWARE在32位中编译,以便能够使用多达4 GB的内存-在64位编译时,该表当然可以更大)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM