3

所以,我正在尝试做一些事情来检查是否自动中了彩票。

我玩的这个游戏有25个号码,你可以选15个。

如果您达到 15 个数字,您就赢了!(它不需要在序列中)

我有一个包含所有游戏的列表:“TodosJogos”,我的赌博名为:“Jogado”

现在,我正在这样做:(我认为这是一个可怕的)

 private void VerificaVitoria(Jogo Jogado, string TipoJogo)
        {
            bool Ganhou;
            List<Jogo> LstClassificadas = new List<Jogo>();

            LstClassificadas = TodosJogos.Where(x => x.N1 == Jogado.N1 ||
                x.N1 == Jogado.N2 ||
                x.N1 == Jogado.N3 ||
                x.N1 == Jogado.N4 ||
                x.N1 == Jogado.N5 ||
                x.N1 == Jogado.N6 ||
                x.N1 == Jogado.N7 ||
                x.N1 == Jogado.N8 ||
                x.N1 == Jogado.N9 ||
                x.N1 == Jogado.N10 ||
                x.N1 == Jogado.N11 ||
                x.N1 == Jogado.N12 ||
                x.N1 == Jogado.N13 ||
                x.N1 == Jogado.N14 ||
                x.N1 == Jogado.N15).ToList<Jogo>();

            LstClassificadas = LstClassificadas.Where(x => x.N2 == Jogado.N1 ||
               x.N2 == Jogado.N2 ||
               x.N2 == Jogado.N3 ||
               x.N2 == Jogado.N4 ||
               x.N2 == Jogado.N5 ||
               x.N2 == Jogado.N6 ||
               x.N2 == Jogado.N7 ||
               x.N2 == Jogado.N8 ||
               x.N2 == Jogado.N9 ||
               x.N2 == Jogado.N10 ||
               x.N2 == Jogado.N11 ||
               x.N2 == Jogado.N12 ||
               x.N2 == Jogado.N13 ||
               x.N2 == Jogado.N14 ||
               x.N2 == Jogado.N15).ToList<Jogo>();

            LstClassificadas = LstClassificadas.Where(x => x.N3 == Jogado.N1 ||
                 x.N3 == Jogado.N2 ||
                 x.N3 == Jogado.N3 ||
                 x.N3 == Jogado.N4 ||
                 x.N3 == Jogado.N5 ||
                 x.N3 == Jogado.N6 ||
                 x.N3 == Jogado.N7 ||
                 x.N3 == Jogado.N8 ||
                 x.N3 == Jogado.N9 ||
                 x.N3 == Jogado.N10 ||
                 x.N3 == Jogado.N11 ||
                 x.N3 == Jogado.N12 ||
                 x.N3 == Jogado.N13 ||
                 x.N3 == Jogado.N14 ||
                 x.N3 == Jogado.N15).ToList<Jogo>();

            //.....

            Ganhou = LstClassificadas.Count > 0 ? true : false;


            if (Ganhou)
            {
                MessageBox.Show("You won in the game: " + TipoJogo);
            }

        }

它类似于此查询:

select * from jogos
where n1 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n2 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n3 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n4 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n5 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n6 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n7 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n8 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n9 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n10 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n11 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n12 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n13 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n14 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)
and n15 in(8,18,16,7,5,20,9,6,10,17,22,3,14,21,15)

有没有更好的方法呢?

4

2 回答 2

32

两种方法来解决你的问题。

将获胜组合表示为一组十五个整数:

HashSet<int>

您有一系列获胜游戏:

IEnumerable<HashSet<int>>

和一个特定的HashSet<int>. 您想知道该特定组合是否与任何获胜组合完全匹配。

方法一

static bool DidIWin(IEnumerable<HashSet<int>> winningNumbers, HashSet<int> myNumbers)
{
    return winningNumbers
      .Where(winningNumber => myNumbers.SetEquals(winningNumber))
      .Any();
}

甚至

static bool DidIWin(IEnumerable<HashSet<int>> winningNumbers, HashSet<int> myNumbers)
{
    return winningNumbers
      .Any(winningNumber => myNumbers.SetEquals(winningNumber));
}

方法二

static bool DidIWin(IEnumerable<HashSet<int>> winningNumbers, HashSet<int> myNumbers)
{
    return false;
}

方法二要快很多。但是,它平均每 300 万个中奖号码中出现一次错误结果。这表明,有时你可以通过愿意接受少量的不准确来获得巨大的性能胜利。

:-)

于 2013-04-16T19:23:22.980 回答
3

从您的 SQL 查询来看,我认为这就是您想要的:

var lotteryNumbers = new int[] { ... };
var results =
    from j in jogos
    where lotteryNumbers.Contains(j.n1) &&
          lotteryNumbers.Contains(j.n2) &&
          lotteryNumbers.Contains(j.n3) &&
          lotteryNumbers.Contains(j.n4) &&
          lotteryNumbers.Contains(j.n5) &&
          lotteryNumbers.Contains(j.n6) &&
          lotteryNumbers.Contains(j.n7) &&
          lotteryNumbers.Contains(j.n8) &&
          lotteryNumbers.Contains(j.n9) &&
          lotteryNumbers.Contains(j.n10) &&
          lotteryNumbers.Contains(j.n11) &&
          lotteryNumbers.Contains(j.n12) &&
          lotteryNumbers.Contains(j.n13) &&
          lotteryNumbers.Contains(j.n14) &&
          lotteryNumbers.Contains(j.n15)
    select j;

或流利的语法

var results =
    jogos.Where(j =>
          lotteryNumbers.Contains(j.n1) &&
          lotteryNumbers.Contains(j.n2) &&
          lotteryNumbers.Contains(j.n3) &&
          lotteryNumbers.Contains(j.n4) &&
          lotteryNumbers.Contains(j.n5) &&
          lotteryNumbers.Contains(j.n6) &&
          lotteryNumbers.Contains(j.n7) &&
          lotteryNumbers.Contains(j.n8) &&
          lotteryNumbers.Contains(j.n9) &&
          lotteryNumbers.Contains(j.n10) &&
          lotteryNumbers.Contains(j.n11) &&
          lotteryNumbers.Contains(j.n12) &&
          lotteryNumbers.Contains(j.n13) &&
          lotteryNumbers.Contains(j.n14) &&
          lotteryNumbers.Contains(j.n15));

lotteryNumbers尽管这假设您在 any或 in 中没有重复项j.n1 .. j.n15。否则你可能会得到意想不到的结果。

于 2013-04-16T18:38:42.883 回答