【问题标题】:comparing two arrays with only loops比较两个数组,只有循环
【发布时间】:2020-07-20 19:39:51
【问题描述】:

我被数组困住了,需要一些帮助。

我有两个数组,我正在使用 foreach 循环对其进行比较。我希望程序仅在winningNumbers 中有两个数字与someNumbers 匹配时才写“你赢了”。如果只有一个匹配,输出应该是“你输了”

(我必须使用循环来解决这个问题)

int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
bool winning = false;

foreach (var item1 in someNumbers)
{
    foreach (var item2 in winningNumbers)
    {
        if (item1 == item2)
        {
            winning = true;
        }
    }
}

if (winning == true )
{
    Console.WriteLine("You won");
}

else
{
    Console.WriteLine("You lost");
}

【问题讨论】:

  • 我能想到的最好的方法是如果我设置 intwinning = 0;并获胜++;在 foreach 循环中。
  • 您显然需要跟踪到目前为止找到了多少匹配项。这个问题对于家庭作业问题来说有点太宽泛了。试着一步一步写下你如何确定获胜状态的逻辑,并将这些步骤转换为 C# 代码。
  • 是的,我想我们现在在同一个页面上?赢了++;我可以计数,然后使用 if 循环输出

标签: c# arrays compare


【解决方案1】:

为每场比赛使用一个计数器。如果它 >= 2,则它报告胜利。如果您要求它恰好是两个匹配项,请删除 > 运算符。在这里试试:https://dotnetfiddle.net/sjYbYk

编辑:添加字符串生成器以报告获胜比赛。

using System;
using System.Text;
                    
public class Program
{
    public static void Main()
    {
            int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
            int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
            int matched = 0;
            StringBuilder sb = new StringBuilder();
            sb.Append("You Matched ");
            foreach (var item1 in someNumbers)
            {
                foreach (var item2 in winningNumbers)
                {
                    if (item1 == item2)
                    {
                        matched++;
                        sb.Append(item1 + ",");
                    }
                }
            }

            if (matched >= 2 )
            {
                Console.WriteLine("You won");
                Console.WriteLine(sb.ToString().TrimEnd(','));
            }

            else
            {
                Console.WriteLine("You lost");
            }
    }
}

【讨论】:

    【解决方案2】:

    由于您正在循环遍历每个数组,因此每次遇到匹配项时都可以增加一个计数器。

                int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
                int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
                int matches = 0;
    
                foreach (var item1 in someNumbers)
                {
                    foreach (var item2 in winningNumbers)
                    {
                        if (item1 == item2)
                        {
                            matches++;
                        }
                    }
                }
    
                if (matches == 2 )
                {
                    Console.WriteLine("You won");
                }
    
                else
                {
                    Console.WriteLine("You lost");
                }
    

    【讨论】:

      【解决方案3】:

      我意识到你的数组都是按升序排列的。如果是这种情况,我更喜欢使用两个指针的方法来获得更好的性能。

      时间复杂度 - O(m+n) :其中 m,n 是两个数组的长度。

      如果您没有排序数组,您可以使用其他答案中提到的蛮力方法,或者先对数组进行排序,然后使用以下方法。

      蛮力的时间复杂度 - O(m*n)

      排序的时间复杂度和两个指针方法 - O(mLogm + nLogn)

      int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
      int[] winningNumbers = { 1, 2, 13, 14, 15, 16 };
      int matches = 0;
      int i=0,j=0;
      while(i<someNumbers.Length && j<winningNumbers.Length)
      {
         if(someNumbers[i]==winningNumbers[j])
         {
             matches++;
             i++;
             j++;
         }
         else if(someNumbers[i]<winningNumbers[j])
         {
             i++;
         }
         else if(someNumbers[i]>winningNumbers[j])
         {
             j++;
         }
      }
      
      if (matches >=2 )
      {
          Console.WriteLine("You won");
       }
       else
       {
           Console.WriteLine("You lost");
       }
      

      【讨论】:

        【解决方案4】:

        根据您的代码,只会打印一个案例。 将结果保存在一个新数组中,然后打印出来。

                int[] someNumbers = { 1, 2, 3, 4, 5, 6 };
                int[] winningNumbers = { 1, 2, 13, 14, 15, 12};
                ArrayList compare_result = new ArrayList();
                //two loop to compare someNumbers and winningNumbers
                for (int x = 0; x < someNumbers.Length; x++)
                {
                    for (int y = 0; y < winningNumbers.Length; y++)
                    {
                        if (someNumbers[x] == winningNumbers[y])
                        {
                            compare_result.Add(someNumbers[x]);
                        }
                    }
                }
                //print comparing result
               if (compare_result.Count > 0)
                {
                    Console.WriteLine("you win:");
                    for (int i = 0; i < compare_result.Count; i++)
                    {
                        Console.WriteLine(compare_result[i]);
                    }
                }
                else
                {
                    Console.WriteLine("You lost");
                }
           
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-16
          • 2016-12-03
          相关资源
          最近更新 更多