1 回答
TA贡献2019条经验 获得超9个赞
伪代码:
Ask for `int ticketCount`
Ask for `int customerCount`
customersWithTickets = List<int>[customerCount] (so an array of customers, each element is the list of the tickets of the customer)
set each element of customersWithTickets to a `new List<int>()`
int remainingTickets = ticketCount
int currentTicketNumber = 1
while remainingTickets != 0
for each customersWithTickets
add to current customersWithTickets the value of currentTicketNumber
increment currentTicketNumber
decrement remainingTickets
if remainingTickets == 0 then break the for cycle
end for
end while
for each customersWithTickets i = [0..customersWithTickets.Length[
print customer#, without going to new line
for each ticket of the current customersWithTickets j = [0..customersWithTickets[i].Count[
print ticket# (`customersWithTickets[i][j]`), without going to new line
end for
print new line
end for
print the customers, each one with its ticket.
您显然可以采取另一种方式(我们将这种方式称为“作弊”方式):您实际上不需要存储客户的单张票来打印它们。您可以简单地注意到,如果有 13 张票并且有 5 个客户,则每个客户有 13 / 5 = 2 张票(其中 / 是整数除法),加上 13 mod 5 = 3(mod 是除法的余数,%在 C# 中)前 3 个客户有一张额外的票。对于门票的确切数量,它甚至更容易:
the customer 1 will have 3 tickets: 1, 6, 11
the customer 2 will have 3 tickets: 2, 7, 12
the customer 3 will have 3 tickets: 3, 8, 13
the customer 4 will have 2 tickets: 4, 9
the customer 5 will have 2 tickets: 5, 10
希望大家清楚,每个客户都有票的形式:
the customer x will have n tickets (calculated as above):
(x + 0 * num of customers),
(x + 1 * num of customers),
(x + 2 * num of customers),
...
- 1 回答
- 0 关注
- 151 浏览
添加回答
举报