Give an array and a certain number,how to select some of the elements to sums upto the number?
The following codes can bring you one or more solutions out:
-
SubSolve(ByValTotalAsDouble,ByRefData,OptionalByValfirstsolutionAsBoolean=True)
-
DimFitAsBoolean,CountAsLong,Result()AsString,Used()AsByte,nAsLong,TempAsDouble
-
n=UBound(Data)
-
ReDimUsed(n)
-
Do
-
Fit=False
-
Do
-
Fori=0Ton
-
Used(i)=1-Used(i)
-
IfUsed(i)=1ThenExitFor
-
Next
-
Ifi>nThenExitDo
- Temp=0
-
Fori=0Ton
-
IfUsed(i)=1ThenTemp=Temp+Data(i)
-
Next
-
IfAbs(Temp-Total)<0.01Then
-
Fit=True
-
ExitDo
-
EndIf
-
Loop
-
IfFitThen
-
Count=Count+1
-
ReDimPreserveResult(1ToCount)
-
Fori=0Ton
-
IfUsed(i)=1ThenResult(Count)=Result(Count)&"+"&Data(i)
-
Next
-
Result(Count)="Solution"&Count&":"&Total&"="&Mid(Result(Count),2)
-
Debug.PrintResult(Count)
-
Iffirstsolution=TrueThenMsgBox"1solutionhasbeenfound!!!":ExitSub
-
EndIf
-
LoopWhileFit
-
MsgBoxIIf(Count>1,Count&"solutionshave",IIf(Count=0,"No",1)&"solutionhas")&"beenfound!!!"
-
EndSub
-
SubGetit()
- Solve1839.31,Array(466.89,480.49,281.06,13.61,70.59,174.86,313.82,374.66,281.5,48.95,262.86,300.33,248.56,184.4,442.91,378.47,196.49,150.7,162.87,307.73,64.97,132.79,480.06,361.54,240.13)
-
EndSub
It returns:
Solution1: 1839.31=13.61+70.59+174.86+313.82+248.56+442.91+378.47+196.49
If we change the codes as the following:
-
SubGetit()
-
Solve1839.31,Array(466.89,480.49,281.06,13.61,70.59,174.86,313.82,374.66,281.5,48.95,262.86,300.33,248.56,184.4,442.91,378.47,196.49,150.7,162.87,307.73,64.97,132.79,480.06,361.54,240.13),False
-
EndSub
We'll get 29 solutions as the following:
Solution1: 1839.31=13.61+70.59+174.86+313.82+248.56+442.91+378.47+196.49
Solution2: 1839.31=466.89+281.06+13.61+70.59+281.5+378.47+196.49+150.7
Solution3: 1839.31=480.49+13.61+70.59+174.86+262.86+378.47+150.7+307.73
Solution4: 1839.31=281.06+13.61+70.59+174.86+313.82+281.5+48.95+196.49+150.7+307.73
Solution5: 1839.31=466.89+480.49+48.95+184.4+442.91+150.7+64.97
Solution6: 1839.31=480.49+13.61+174.86+313.82+281.5+196.49+150.7+162.87+64.97
Solution7: 1839.31=13.61+374.66+300.33+184.4+442.91+150.7+307.73+64.97
Solution8: 1839.31=13.61+313.82+48.95+300.33+248.56+378.47+162.87+307.73+64.97
Solution9: 1839.31=480.49+70.59+281.5+48.95+262.86+248.56+150.7+162.87+132.79
Solution10: 1839.31=281.06+13.61+70.59+281.5+300.33+184.4+196.49+150.7+162.87+64.97+132.79
Solution11: 1839.31=281.06+262.86+248.56+378.47+162.87+307.73+64.97+132.79
Solution12: 1839.31=466.89+70.59+174.86+48.95+262.86+184.4+150.7+480.06
Solution13: 1839.31=281.06+13.61+70.59+281.5+300.33+196.49+150.7+64.97+480.06
Solution14: 1839.31=281.06+13.61+174.86+262.86+300.33+248.56+196.49+361.54
Solution15: 1839.31=281.06+13.61+174.86+313.82+248.56+184.4+196.49+64.97+361.54
Solution16: 1839.31=70.59+174.86+281.5+48.95+378.47+150.7+307.73+64.97+361.54
Solution17: 1839.31=13.61+70.59+442.91+162.87+307.73+480.06+361.54
Solution18: 1839.31=70.59+48.95+300.33+248.56+196.49+132.79+480.06+361.54
Solution19: 1839.31=466.89+281.06+70.59+174.86+442.91+162.87+240.13
Solution20: 1839.31=374.66+281.5+248.56+184.4+196.49+150.7+162.87+240.13
Solution21: 1839.31=281.06+13.61+174.86+374.66+262.86+184.4+307.73+240.13
Solution22: 1839.31=466.89+70.59+174.86+48.95+262.86+196.49+150.7+162.87+64.97+240.13
Solution23: 1839.31=281.06+374.66+281.5+378.47+150.7+132.79+240.13
Solution24: 1839.31=70.59+374.66+48.95+184.4+307.73+132.79+480.06+240.13
Solution25: 1839.31=281.06+13.61+174.86+48.95+248.56+162.87+307.73+361.54+240.13
Solution26: 1839.31=480.49+13.61+70.59+174.86+300.33+64.97+132.79+361.54+240.13
Solution27: 1839.31=13.61+281.5+48.95+262.86+248.56+184.4+64.97+132.79+361.54+240.13
Solution28: 1839.31=313.82+262.86+300.33+162.87+64.97+132.79+361.54+240.13
Solution29: 1839.31=281.06+70.59+374.66+150.7+162.87+64.97+132.79+361.54+240.13
分享到:
相关推荐
SUMS30 Mathematics for Finance -- An Introduction to Financial Engineering, Marek Capiński, Tomasz Zastawniak (2003).pdf
《SUMS62:拉普拉斯变换与傅里叶级数导论》是Phil Dyke在2014年撰写的一本数学教材,主要涵盖了拉普拉斯变换和傅里叶级数这两个重要的数学工具。拉普拉斯变换和傅里叶级数在工程、物理、信号处理等领域具有广泛的...
SUMS26 Matrix Groups -- An Introduction to Lie Group Theory, Andrew Baker (2002) .djvu
《SUMS08 Elementary Number Theory》是由Gareth A. Jones和J. Mary Jones在1998年合著的一本关于初等数论的教材。这本书深入浅出地介绍了数论的基本概念、定理和应用,是学习和理解数论的理想入门资料。书中的内容...
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four ...
《SUMS26 Matrix Groups -- An Introduction to Lie Group Theory》是由数学家Andrew Baker在2002年编写的,这是一本深入浅出地介绍李群理论的教材。李群在数学,特别是抽象代数和理论物理中占有重要的地位,是理解...
总的来说,《SUMS79 From Natural Numbers to Quaternions》是数学爱好者和专业人士探索数系奥秘的一份宝贵资源,它展示了数学从简单到复杂,从实际到抽象的美妙历程,对于深化对数的理解和应用具有重要意义。...
The cows in farmer John's herd are ...Write a program that will compute the number of ways farmer John can sum the numbers on consecutive cows to equal N. Do not use precomputation to solve this problem.
SUMS82 A History of Abstract Algebra -- From Algebraic Equations to Modern Algebra, Jeremy Gray (2018).zip
elements-branches and itself be an element to other parental record. Component TDBGridEh supports to show the tree-type structure of these records. Allows to connect to the internal array of ...
《SUMS48 Introduction to Analytical Dynamics, Revised Edition》是由Nicholas Woodhouse在2009年出版的一本关于分析动力学的教材。这本书深入浅出地介绍了这个领域中的核心概念,是数学和物理学生以及相关专业...
elements-branches and itself be an element to other parental record. Component TDBGridEh supports to show the tree-type structure of these records. Allows to connect to the internal array of ...
elements-branches and itself be an element to other parental record. Component TDBGridEh supports to show the tree-type structure of these records. Allows to connect to the internal array of ...
elements-branches and itself be an element to other parental record. Component TDBGridEh supports to show the tree-type structure of these records. Allows to connect to the internal array of ...
4. 在文件内容中还提到了其他一些SUMS系列的书籍,例如 "Basic Linear Algebra","Calculus and Geometry: Multivariate","Elements of Logic via Numbers and Sets","Elementary Number Theory" 等。这些书籍涵盖...
elements-branches and itself be an element to other parental record. Component TDBGridEh supports to show the tree-type structure of these records. Allows to connect to the internal array of ...
标题《SUMS04 Elements of Logic via Numbers and Sets, D. L. Johnson (1998).pdf》和描述《Springer Undergraduate Mathematics Series(SUMS)》中包含的文本提供了几个重要的知识点,涉及数学逻辑、数论、数学教育...
标题“POJ2602-Superlong sums”是指北京大学在线编程平台POJ(Peking University Online Judge)上的一道题目,编号为2602,名为“超级长和”。这是一道与数值计算和算法相关的编程挑战,通常涉及到大数据处理和高...