Math 如何计算循环所需的元素?
我有以下数据: y-n-y-y-n-n-n 这会无限重复,例如: y-n-y-y-n-n-n-y-n-n-n-n-y-n-n-y-n-n-n-n-n-n-n-n-n-n-n 我有5“x”。 “x”只跟“y”在一起。 也就是说,如果我在上面的循环中分配x,它将是:Math 如何计算循环所需的元素?,math,formula,equation,Math,Formula,Equation,我有以下数据: y-n-y-y-n-n-n 这会无限重复,例如: y-n-y-y-n-n-n-y-n-n-n-n-y-n-n-y-n-n-n-n-n-n-n-n-n-n-n 我有5“x”。 “x”只跟“y”在一起。 也就是说,如果我在上面的循环中分配x,它将是: y-n-y-y-n-n-n-y-n-y-n-n-n-n x--x-x--x-x 我想数一数我需要使用多少个循环元素来分布5x,答案是10 我怎么用公式计算呢?我想你说的是,你需要处理无限列表的前10个元素,得到5个Y,与你拥有的5个X相
y-n-y-y-n-n-n-y-n-y-n-n-n-n
x--x-x--x-x
我想数一数我需要使用多少个循环元素来分布5x,答案是10
我怎么用公式计算呢?我想你说的是,你需要处理无限列表的前10个元素,得到5个Y,与你拥有的5个X相匹配
y-n-y-y-n-n-n-y-n-y-y-n-n-n-y-n-y-y-n-n-n...
x-_-x-x-_-_-_-x-_-x
^
L____ 10 elements read from the infinite list to place the 5 x's.
我还假设您的问题是:给定5X的输入,您需要在无限列表中处理多少个元素来匹配这些5X
您可以使用类似以下伪代码的循环来计算它:
iElementsMatchedCounter = 0
iXsMatchedCounter = 0
iXLimit = 5
strElement = ""
if (InfiniteList.IsEmpty() == false)
{
do
{
strElement = InfiniteList.ReadNextElement()
if (strElement == "y")
{
iXsMatchedCounter += 1
}
iElementsMatchedCounter += 1
} while ( (InfiniteList.IsEndReached() == false) AND (iXsMatchedCounter < iXLimit) )
}
if (iXsMatchedCounter = iXLimit)
then Print(iElementsMatchedCounter)
else Print("End of list reached before all X's were matched!")
你能把你的问题解释清楚吗?
iQuotient = iNumberOfXs / iNumberOfYsInSubList // COMMENT: here integer division automatically drops the remainder
iPartialLengthOfList = iQuotient * iLengthOfSubList
iPendingXs = iNumberOfXs - (iQuotient * iNumberOfYsInSubList)
// COMMENT: can use modulo arithmetic like the following to calculate iPendingXs
// iPendingXs = iNumberOfXs % iNumberOfYsInSubList
// The following IF statement assumes the sub-list to be y-n-y-y-n-n-n
if (iPendingXs > 1)
then iPendingLengthOfList = iPendingXs + 1
else iPendingLengthOfList = iPendingXs
iResult = iPartialLengthOfList + iPendingLengthOfList