1 - 1
2 - 2,3
3 - 4,5,6
4 - 7,8,9,10
鑑於從4至6的任何數字,我所需要的輸出爲3。有效的數據結構和算法 - 天然序列
鑑於從7任意數量爲10,我需要的輸出爲4。
我需要解決上述問題的最快解決方案來解決算法。
我能想到的是蠻力算法:
鑑於7:
n-square + n = 7*2 = 14
1 + 1 = 2 < 14
4 + 2 = 6 < 14
9 + 3 = 12 < 14
16+ 4 = 20 >=14 --> So 4
有沒有更好的方法在解決辦法?或者我對算法本身的方法有缺陷?該算法中的
簡要說明:
A,B,C
每次迭代之後的每個元素變爲增加一。
A,A,B,B,C,C
鑑於3,將返回C.
鑑於4或5,將返回A.
給定6或7,B將被退回。
給定8或9,將返回C.
給定10或11或12,將返回A.
給定13或14或15,B將被退回。
如何解決數學問題將有助於解決ALGO:
Total number of elements = 3
Given number = 13 (Output to be B)
Divide and Ceiling = Ceil (13/3) = 5 [So 13 falls under when every element has become * 3] (From Mathematical problem : If given number is 5, 3 is to be used)
Starting index of when every element has become * 3 [IS_EQUAL_TO = ] 3 * 3(summation of previous iteration => 1 + 2) + 1 = 10
To Find the index = Ceil(13-10+1/3 (this 3,comes from the mathematical problem)) = Ceil (4/3) = 2nd index = B
是否有隱藏在某處的問題? –
@JBNizet我有一個問題。請看看我現在是否有意義。 – user104309
您是否正在尋找適用於任何數字(例如5654357)或僅適用於1-10的通用數學公式?因爲,如果它只是1-10,一個簡單的查找表就可以了,而且效率非常高。 –