为了账号安全,请及时绑定邮箱和手机立即绑定

每个客户练习的 C# 票分配

每个客户练习的 C# 票分配

C#
拉丁的传说 2021-08-29 17:41:22
我需要你的帮助来完成这个练习。我需要为每个客户分发票。如果票多于客户,票将被添加到第一个客户,依此类推。例子:Enter ticket number: 10Enter customer number: 5结果:customer#1 ticket#1 ticket#6customer#2 ticket#2 ticket#7customer#3 ticket#3 ticket#8customer#4 ticket#4 ticket#9customer#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]);            }        }    }}谢谢大家
查看完整描述

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), 

    ...


查看完整回答
反对 回复 2021-08-29
  • 1 回答
  • 0 关注
  • 151 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信