猿问

每个客户练习的 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]);

            }

        }

    }

}

谢谢大家


拉丁的传说
浏览 152回答 1
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 = ticketCountint currentTicketNumber = 1while remainingTickets != 0&nbsp; &nbsp; for each customersWithTickets&nbsp; &nbsp; &nbsp; &nbsp; add to current customersWithTickets the value of currentTicketNumber&nbsp; &nbsp; &nbsp; &nbsp; increment currentTicketNumber&nbsp; &nbsp; &nbsp; &nbsp; decrement remainingTickets&nbsp; &nbsp; &nbsp; &nbsp; if remainingTickets == 0 then break the for cycle&nbsp; &nbsp; end forend whilefor each customersWithTickets i = [0..customersWithTickets.Length[&nbsp; &nbsp; print customer#, without going to new line&nbsp; &nbsp; for each ticket of the current customersWithTickets j = [0..customersWithTickets[i].Count[&nbsp; &nbsp; &nbsp; &nbsp; print ticket# (`customersWithTickets[i][j]`), without going to new line&nbsp; &nbsp; end for&nbsp; &nbsp; print new lineend forprint 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&nbsp;the customer 2 will have 3 tickets: 2, 7, 12the customer 3 will have 3 tickets: 3, 8, 13the customer 4 will have 2 tickets: 4, 9the customer 5 will have 2 tickets: 5, 10希望大家清楚,每个客户都有票的形式:the customer x will have n tickets (calculated as above):&nbsp;&nbsp; &nbsp; (x + 0 * num of customers),&nbsp;&nbsp; &nbsp; (x + 1 * num of customers),&nbsp;&nbsp; &nbsp; (x + 2 * num of customers),&nbsp;&nbsp; &nbsp; ...
随时随地看视频慕课网APP
我要回答