如何检查彩票中的等值?

How do I check for equals in my lottery ticket?

提问人:someone 提问时间:10/21/2022 最后编辑:M. Justinsomeone 更新时间:11/8/2023 访问量:111

问:

在课堂上,我们目前正在制作彩票作为一个项目,我目前被困在一个平等的问题上: 同一行不允许在同一张票证上多次出现。

我尝试了几件事,但没有任何效果。

public class TalRækker //rows
{
    static Random rnd = new Random();
    public static int[,] Line()
    {
        int[,] line = new int[10, 7];
        //int x = rnd.Next(1, 36);

        for (int i = 0; i < line.GetLength(0); i++)
        {
            for (int j = 0; j < line.GetLength(1); j++)
            {
                line[i, j] = rnd.Next(1, 36);
            }
        }
        return line;


    }
    public static void Print2DArray<T>(T[,] line)
    {
        var lineNumber = 1;
        for (int i = 0; i < line.GetLength(0); i++)
        {
            Console.Write(" {0:D2}.\t", lineNumber);
            for(int j = 0; j < line.GetLength(1); j++)
            {
                Console.Write("{0:D2} ", line[i, j]);
            }
            Console.WriteLine();
            lineNumber++;
        }
    }

我已将代码更新为以下内容,但仍然遇到问题:

namespace lottery 
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\t  Lotto " + DateTime.Now.ToString("dd/MM/yyyy"));
            Console.WriteLine("\t" + "\t1-uge");             
            Console.WriteLine("\t      LYN-LOTTO\t");             
            Console.WriteLine(" ");            
            TalRækker.Print2DArray(TalRækker.?()); //here is the problem
            Console.WriteLine("");
        }
    }
}
C# 相等

评论

1赞 Matthew Watson 10/21/2022
我假设同一行中不允许多次使用相同的数字?你也没有检查那个。

答:

1赞 sr28 10/21/2022 #1

我很想使用锯齿状数组来使其更容易处理。然后,我将所需的功能拆分为单独的方法,以便在存在重复项时允许递归:

    static Random rnd = new Random();
    public static int[][] GenerateTicket(int numberOfRows, int rowLength)
    {
        var lines = new int[numberOfRows][];

        for (int i =0; i < lines.Length; i++)
        {
            lines[i] = GenerateLine(rowLength, lines);
        }

        return lines;
    }

    public static int[] GenerateLine(int rowLength, int[][] existingLines)
    {
        var newLine = new int[rowLength];
        for (int j = 0; j < rowLength; j++)
        {
            newLine[j] = GenerateNumber(newLine);
        }

        var orderedLine = newLine.OrderBy(n => n).ToArray();
        var exists = existingLines.Where(line => line != null).Any(line => line.SequenceEqual(orderedLine));

        // If a line already exists, call the method again to generate a new line, otherwise return the line.
        return exists ? GenerateLine(rowLength, existingLines) : orderedLine;
    }

    public static int GenerateNumber(int[] line)
    {
        var number = rnd.Next(1, 36);
        // Check if the line already contains the number, if so, call the method again to generate a new number, otherwise return the number.
        return line.Contains(number) ? GenerateNumber(line) : number;
    }

    public static void Print2DArray<T>(T[][] lines)
    {
        var lineNumber = 1;
        for (int i=0; i < lines.Length; i++)
        {
            Console.Write(" {0:D2}.\t", lineNumber);
            lineNumber++;
            for (int j = 0; j < lines[i].Length; j++)
            {
                Console.Write("{0:D2} ", lines[i][j]);
            }
            Console.WriteLine();
        }
    }

更新

在使用 SequenceEqual 之前,我已经对 newLine 进行了排序,以便将先前排序的行与此行进行比较,从而正确返回 true/false。

评论

0赞 someone 10/25/2022
嗨,谢谢你的帮助,我喜欢这个解决它的 wau,对我来说作为一个新手更有意义。问题是,当我尝试从我的主程序中调用它时,我无法让它按照您制作的方式工作。
0赞 someone 10/25/2022
代码,我是网站的新手,所以以代码格式制作它会让 xD 感到困惑code namespace lottery { class Program { static void Main(string[] args) { Console.WriteLine("\t Lotto " + DateTime.Now.ToString("dd/MM/yyyy")); Console.WriteLine("\t" + "\t1-uge"); Console.WriteLine("\t LYN-LOTTO\t"); Console.WriteLine(" "); TalRækker.Print2DArray(TalRækker.?());//here is the problem Console.WriteLine(""); } } }
0赞 sr28 10/25/2022
在尝试打印任何内容之前,您需要使用“GenerateTicket”。
0赞 Emre Utku Solak 10/21/2022 #2

我假设这是一个数组练习。否则,我宁愿使用列表。

因此,您可以执行类似于以下内容的操作。请注意每个函数如何遵循单一责任原则和访问修饰符的使用。

这是 Do-While 循环的一个很好的用例,因为您的条件取决于行生成的第一个结果。

我没有检查连续出现相同数字的情况,因为您没有在问题中说明这一点。

public class TalRækker
{
    static Random rnd = new Random();
    public static int[,] GenerateLotteryTicket(int totalLines, int lineLength)
    {
        
        int[,] lines = new int[totalLines, lineLength];
        int[] newLine = new int[lineLength];


        for (int i = 0; i < totalLines; i++)
        {
            do
            {
                newLine = GenerateLine(lineLength);
            }
            while (ExistInTicket(lines,newLine));

            for (int j = 0; j < lineLength; j++)
            {
                lines[i,j] = newLine[j];
            }
        }
        return lines;
    }

    private static int[] GenerateLine(int lineLength)
    {
        int[] row = new int[lineLength];
        for (int i = 0; i < lineLength; i++)
        {
            row[i] = rnd.Next(1, 36);
        }
        return row;
    }
    private static bool ExistInTicket(int[,] ticket, int[] line)
    {
        bool rowMatchFound;
        for(int i = 0; i < ticket.GetLength(0); i++)
        {
            rowMatchFound = true;
            for (int j = 0; j < ticket.GetLength(1); j++)
            {
                if(ticket[i,j] != line[j])
                {
                    rowMatchFound = false;
                    break;
                }
            }
            if(rowMatchFound) return true;
        }
        return false;
    }

    public static void Print2DArray<T>(T[,] line)
    {
        var lineNumber = 1;
        for (int i = 0; i < line.GetLength(0); i++)
        {
            Console.Write(" {0:D2}.\t", lineNumber);
            for (int j = 0; j < line.GetLength(1); j++)
            {
                Console.Write("{0:D2} ", line[i, j]);
            }
            Console.WriteLine();
            lineNumber++;
        }
    }
}
0赞 Idle_Mind 11/8/2023 #3

将数字 1 到 35 放入列表中。对于每一行,随机对数字进行排序,然后取前 7 个。通过对这些数字进行排序并用逗号等分隔符分隔它们来从这些数字构建“密钥”。如果该键在 HashSet 中不存在,则它是唯一的行。将其添加到 HashSet 并将数字复制到您的数组中。重复上述步骤,直到获得所有 10 行:

public static int[,] Line()
{
    int[,] line = new int[10, 7];
    HashSet<String> rows = new HashSet<String>();
    var numbers = Enumerable.Range(1, 35).ToList();            
    for (int i = 0; i < line.GetLength(0); i++)
    {
        int[] row;
        String rowKey;
        do
        {
            row = numbers.OrderBy(x => rnd.Next())
                .Take(line.GetLength(1)).ToList()
                .OrderBy(x => x).ToArray();
            rowKey = String.Join(",", row);
        } while (rows.Contains(rowKey));                
        rows.Add(rowKey);

        for (int j = 0; j < line.GetLength(1); j++)
        {
            line[i, j] = row[j];
        }
    }
    return line;
}