无需辅助单元格的数组公式求解八城市既定行程距离问题
Hey there! Let's tackle this problem step by step. First, let's map out the exact segment distances from your given data and the fixed itinerary (A→B→C→D→E→F→G→H):
- A→B (Segment 1): 200
- B→C (Segment 2): 350
- C→D (Segment 3): 500
- D→E (Segment 4): 250
- E→F (Segment 5): 850
- F→G (Segment 6): 1250
- G→H (Segment 7): 150
I'll cover two common use cases based on what you might need from the parameter k:
1. Get the distance of the k-th individual segment
If you want to pull just the distance for the k-th leg of the trip (where k is between 1 and 7), this simple formula does the trick without any helper cells or data changes:
=CHOOSE(k, 200, 350, 500, 250, 850, 1250, 150)
It directly maps the k value to the corresponding segment distance—super straightforward.
2. Get the cumulative total distance after k segments
If you need the total miles driven after completing the first k segments, here are two solid options:
Option A (Concise dynamic array version)
For modern Excel versions that support dynamic arrays, this formula is clean and efficient:
=SUM(INDEX({200,350,500,250,850,1250,150},,1:k))
The INDEX function grabs the first k elements from our segment distance array, then SUM adds them up.
Option B (Compatible with older Excel versions)
If you're using an older Excel build that doesn't handle dynamic arrays well, use this CHOOSE-based approach:
=SUM(CHOOSE(k, 200, {200,350}, {200,350,500}, {200,350,500,250}, {200,350,500,250,850}, {200,350,500,250,850,1250}, {200,350,500,250,850,1250,150}))
It works by selecting the full set of distances up to k segments, then summing them.
All of these formulas stick strictly to your requirements: no helper cells are used, and your original distance data stays completely unmodified.
内容的提问来源于stack exchange,提问作者Mudit Kumar

