在这项练习中,我需要您的帮助。我需要为每个客户分配票证。如果票证超过客户,则该票证将添加到第一个客户,依此类推。
示例:
Enter ticket number: 10
Enter customer number: 5
结果:
customer#1 ticket#1 ticket#6
customer#2 ticket#2 ticket#7
customer#3 ticket#3 ticket#8
customer#4 ticket#4 ticket#9
customer#5 ticket#5 ticket#10
到目前为止,这是我的代码,仅能满足客户的第一个循环,但是后续的问题是我的问题。
List<int> customerNumberList = new List<int>();
List<int> ticketNumberList = new List<int>();
Console.Write("Enter Numer of Tickets: ");
int ticketCount = int.Parse(Console.ReadLine());
Console.Write("Enter Number of Customer: ");
int customerCount = int.Parse(Console.ReadLine());
for(int i = 1; i <= ticketCount; i++)
{
ticketNumberList.Add(i);
}
for(int i = 1; i <= customerCount; i++)
{
customerNumberList.Add(i);
}
if(customerNumberList.Count == 1)
{
Console.WriteLine("Customer#1");
for (int i = 0; i < ticketNumberList.Count; i++)
{
Console.WriteLine("Ticket#" + ticketNumberList[i]);
}
}
else
{
for (int i = 0; i < customerNumberList.Count; i++)
{
Console.WriteLine("Customer#" + customerNumberList[i]);
for(int j = 0; j <ticketNumberList.Count; j++)
{
if(customerNumberList[i] == ticketNumberList[j])
{
Console.WriteLine("Ticket#" + ticketNumberList[j]);
}
}
}
}
谢谢大家
答案 0 :(得分:1)
伪代码:
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),
...