Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

c# - How do I create an array of ascending and then descending numbers?

For example:

I have min and max values and a number of increments which may be odd or even;

if I have min = 3 and max = 10 and increments = 15 then I want:

3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3

However, if increments = 16 I want (notice the two 10's in the middle):

3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3

I have to create these arrays add-hoc using just min, max, and number of increments.

UPDATE:

To make this clearer the number of increments is equal to the number of items that must be in the array and the items are decimals.

so if min = 5.0 and max = 15.0 and increments = 6 then the array would contain:

5.0, 10.0, 15.0, 15.0, 10.0, 5.0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Linq way:

int min = 3;
int max = 10;
int increments = 15;

Enumerable
    .Range(min, max - min + 1)
    .Concat(Enumerable
        .Range(min, max - min + 1)
        .Reverse()
        .Skip(increments % 2))
    .ToArray();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...