計算 1+2+3+... ,當加到多少時,剛好大於 1000
計算 N+(N-1)+(N-2)+... ,當加到多少時,剛好大於 1000
Sub TestSumSoluation()
Dim iiKeyNum As Integer
Dim iiSoulation As Integer
Dim iiThreshold As Integer
Dim iiStep As Integer
Dim BottomLimit As Integer
Dim TopLimit As Integer
'Input
iiStep = 2
BottomLimit = 1
TopLimit = 100
'*********************************************
iiSoulation = 0
iiThreshold = 1000
For iiKeyNum = BottomLimit To TopLimit Step iiStep
iiSoulation = iiSoulation + iiKeyNum
If iiSoulation > iiThreshold Then Exit For
Next 'iiKeyNum = 1 To 1000
Debug.Print iiKeyNum
'**********************************************
iiSoulation = 0
iiThreshold = 1000
For iiKeyNum = TopLimit To BottomLimit Step -iiStep
iiSoulation = iiSoulation + iiKeyNum
If iiSoulation > iiThreshold Then Exit For
Next 'iiKeyNum = 1 To 1000
Debug.Print iiKeyNum
End Sub