Bus Seat Arrangement with Numbers in chart – C# Algorithm – Find Seat is W, M, S from number is inputted

huangapple go评论96阅读模式
英文:

Bus Seat Arrangement with Numbers in chart - C# Algorithm - Find Seat is W, M, S from number is inputted

问题

W M S | S M W
01 02 03 | 19 20 21
04 05 06 | 22 23 24
07 08 09 | 25 26 27
10 11 12 | 28 29 30
13 14 15 | 31 32 33
16 17 18 | 34 35 36

这些是巴士的座位安排。我必须编写一个C#算法 - 当用户输入一个数字时,算法应该返回座位是W,M还是S。例如 - 如果用户输入数字26,算法应该返回M。如何以最快的方式实现这一目标?

英文:
  1. W M S | S M W
  2. 01 02 03 | 19 20 21
  3. 04 05 06 | 22 23 24
  4. 07 08 09 | 25 26 27
  5. 10 11 12 | 28 29 30
  6. 13 14 15 | 31 32 33
  7. 16 17 18 | 34 35 36

These are the seat arrangements for a bus. I have to write a C# algorithm - When user gives a number the algorithm should return the seat is W, M or S. Example - If user enters number 26 algorithm should return M. What will the fastest way to achieve this?

答案1

得分: 5

Sure, here's the translated code:

  1. 这个怎么样?
  2. public static char SeatId(int seatNumber)
  3. {
  4. return seatNumber >= 19
  5. ? "SMW"[(seatNumber-1) % 3]
  6. : "WMS"[(seatNumber-1) % 3];
  7. }
  8. 更短但不太易读:
  9. public static char SeatId(int seatNumber)
  10. {
  11. return --seatNumber >= 18
  12. ? "SMW"[seatNumber % 3]
  13. : "WMS"[seatNumber % 3];
  14. }

Please let me know if you need anything else.

英文:

What about this?

  1. public static char SeatId(int seatNumber)
  2. {
  3. return seatNumber >= 19
  4. ? "SMW"[(seatNumber-1) % 3]
  5. : "WMS"[(seatNumber-1) % 3];
  6. }

Shorter but less readable:

  1. public static char SeatId(int seatNumber)
  2. {
  3. return --seatNumber >= 18
  4. ? "SMW"[seatNumber % 3]
  5. : "WMS"[seatNumber % 3];
  6. }

huangapple
  • 本文由 发表于 2023年6月29日 16:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定