c# - 每个客户练习的 C# 票分配
问题描述
我需要你的帮助来做这个练习。我需要为每个客户分配票。如果票多于客户,则票将添加到第一个客户,依此类推。
例子:
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]);
}
}
}
}
感谢大家
解决方案
伪代码:
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),
...
推荐阅读
- python - 操纵 pandas 数据框中的值以依赖于不同的其他值
- python - Django, RestAPI, Microsoft Azure, 网站, 虚拟机, ubuntu
- javascript - 向 HTML 添加内容
标签
- javascript - 代码没有从 ANTJS v3 升级到 ANTJS v4
- python - 对于循环,为什么索引超出范围?
- reactjs - 带有 Redux 设置的 TypeScript CRA
- scripting - 为什么我在 Delphi 脚本中的记录类型定义上出现语法错误?
- spring - 如何使用 WebClient 压缩 JSON 请求正文?
- java - 下拉菜单未填充,我的错误在哪里?
- python - 如何使用里面的 Python 代码运行可执行文件