| |

VerySource

 Forgot password?
 Register
Search
View: 661|Reply: 1

Find an algorithm.

[Copy link]

2

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-24 13:20:01
| Show all posts |Read mode
A sequence of numbers, such as: 1, 2, 3, 5, 7, 8, 9

The program is required to complete such functions, read this number sequence in a loop, and form this number sequence into a string. The format is required: "1-3,5,7-9". That is, the increasing number sequence is in nm Performance, otherwise it is expressed as n, m.

Is there any good algorithm ?! C #
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-12 15:15:01
| Show all posts
If it's in order, can't iterate over it?

A variable int pre holds the previous number, and it is checked first when it encounters the current number int cur.

if cur-pre == 1 then

pre = cur
next

else

print "-" + pre + "," + cur

end if
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list