使用循环比较两个数组

5
我被数组问题困扰并需要帮助。
我有两个数组,并使用foreach循环进行比较。如果在winningNumbers中有两个数字与someNumbers匹配,程序将写入“you won” 。如果只有一个匹配,输出应为“You lost”。
(我必须使用循环来解决这个问题)
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");
}

我能想到的最好的方法是在foreach循环中设置int winning = 0;和winning++;。 - Nirre
1
你需要清楚地跟踪到目前为止找到了多少匹配项。这个问题对于作业问题来说有点太宽泛了。尝试逐步写下确定胜利状态的逻辑,并将这些步骤转化为C#代码。 - itsme86
嗯,我觉得现在我们意见一致了吧?用 winning++ 可以进行计数,然后使用 if 循环来输出。 - Nirre
4个回答

3

对于每个匹配项使用计数器,如果计数器 >= 2 则报告胜利。如果您要求恰好两个匹配项,则删除 > 操作符。在此处尝试: https://dotnetfiddle.net/sjYbYk

编辑:添加 StringBuilder 以报告获胜的匹配项。

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");
            }
    }
}

3

既然你正在遍历每个数组,你只需每次遇到匹配项时递增计数器即可。

            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");
            }

2
我发现你的数组都是按升序排列的。如果是这样,我更喜欢使用双指针方法来提高性能。
时间复杂度 - 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");
 }

2
根据您的代码,只有一个情况会被打印出来。请将结果保存在一个新数组中,然后再打印出来。
        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");
        }
   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接