我不知道如何检查匹配的数字

本文关键字:数字 检查 何检查 我不知道 | 更新日期: 2023-09-27 18:25:03

我是C#的新手,最近的任务是创建一个基本的彩票程序,允许用户输入六个号码,然后对照一个表进行检查,该表显示了52周的彩票号码,每个号码有6个号码。我已经完成了这部分代码,但还有另一项任务需要我选择用户匹配次数最多的一周,而这正是我真正缺乏代码技能的地方。如有任何帮助,我们将不胜感激。

static void Main(string[] args)
{
    int[] thelottery = new int[52];
    int[] usernumbers = new int[6];
    Random rnd = new Random();
    Console.Write("Please enter six numbers for the lottery: 'n");
    usernumbers[0] = Convert.ToInt32(Console.ReadLine());
    usernumbers[1] = Convert.ToInt32(Console.ReadLine());
    usernumbers[2] = Convert.ToInt32(Console.ReadLine());
    usernumbers[3] = Convert.ToInt32(Console.ReadLine());
    usernumbers[4] = Convert.ToInt32(Console.ReadLine());
    usernumbers[5] = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("'nYour numbers are: ");
    for (int i = 0; i < 6; i++)
    {
        Console.Write("{0} ", usernumbers[i]);
    }
    Console.Write("'n'nPress any key to view the results of the lottery: ");
    Console.ReadKey();
    for (int limit  = 0; limit < 52; limit++)
    {
        for (int weeks = 0; weeks < 6; weeks++)
        {
            thelottery[weeks] = rnd.Next(49) + 1;
            Console.Write("'t{0} ", thelottery[weeks]);
        }
        Console.WriteLine();
    }
    Console.WriteLine("'n'nYour week with the most matches was...");
    Console.WriteLine("'n'nPress any key to continue: ");
    Console.ReadKey();
}

我不知道如何检查匹配的数字

如果您需要一个参考解决方案:

    Console.Write("'n'nPress any key to view the results of the lottery: ");
    Console.ReadKey();
    int best_week = 0;
    int best_matches = 0; 
    /* Compute lottery numbers for 52 weeks */
    for (int limit  = 0; limit < 52; limit++)
    {
        /* Compute the 6 lottery numbers for this week */
        for (int weeks = 0; weeks < 6; weeks++)
        {
            thelottery[weeks] = rnd.Next(49) + 1;
            Console.Write("'t{0} ", thelottery[weeks]);
        }
        /* Compute the amount of matches for this week. */
        int matches = 0; 
        //Check each usernumber with each lottery number for a match
        for(int i=0; i < 6; i++)
        {
            //e.g. for i=0, check how many times usernumbers[0] appears in thelottery[].
            for(int j=0; j < 6; j++)
            {
                if(usernumbers[i] == thelottery[j])
                    matches++;
            }
        }
        /* Did we have more matches this week than previosuly recorded? */
        if(matches > best_matches){
            /* yes, remember the new best week and the number of matches. */
            best_week = limit; 
            bist_matches = matches;
        }
        Console.WriteLine();
    }
    /* Here, the result of the best week and the best matches is avaiable! */

将问题分解为简单的步骤。你需要知道用户在哪一周的比赛次数最多。要做到这一点,你需要了解

  • 用户每周有多少场比赛
  • 那一年的最高比赛次数是多少(在哪一周)

想想你需要什么数据。在这样一个简单的程序中,我可能会根据周数存储匹配次数,即。第1周:0场比赛第2周:3场比赛第4周:6场比赛。。。呜呜!等等

在C#中,最简单的方法是使用Dictionary<int, int>

这应该足以让你自己解决剩下的问题。

认为这种问题非常适合BitArray的And方法。

基本上,usernumbers将是一个BitArray实例,并且您将有一个包含最后52个BitArray lotterynumbers的列表。

static void Main(string[] args)
{
    List<BitArray> lotterynumbers = GetLastWinningNumbers();
    BitArray usernumbers = new BitArray(MAX_LOTTERY_NUMBER);
    Console.Write("Please enter six numbers for the lottery: 'n");
    for (int i = 0; i < 6; i++)
        usernumbers[Convert.ToInt32(Console.ReadLine())] = true;
    BitArray bestmatches = new BitArray(MAX_LOTTERY_NUMBER);
    BitArray bestweek = bestmatches;
    foreach (BitArray weeknumbers in lotterynumbers)
    {
        BitArray currentmatches = usernumbers.And(weeknumbers);
        if (currentmatches.Count > bestmatches.Count)
        {
            bestmatches = currentmatches;
            bestweek = weeknumbers;
        }
    }
    Console.WriteLine("'n'nYour week with the most matches was...");
    foreach (int number in bestweek)
    {
        Console.Write(number);
        Console.Write(' ');
    }
    Console.WriteLine("'n'nPress any key to continue: ");
    Console.ReadKey();
}