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

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

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。如何以最快的方式实现这一目标?

英文:
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

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:

这个怎么样?

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

更短但不太易读:

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

Please let me know if you need anything else.

英文:

What about this?

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

Shorter but less readable:

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

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:

确定