英文:
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];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论