互相比较 7 个单词,看看其中 5 个是否相等。如何?

发布于 2024-08-23 07:41:56 字数 306 浏览 3 评论 0原文

我的数组中有七个单词:

string[7] = {x,x,x,x,x,x,x};

x 是从另一个数组生成的:

string[4]={a,b,c,d};

这意味着每个 x 可以是 a 或 b 或 c 或 d。它是随机生成的。这可能是一个例子:

string[7]= {a,a,d,a,a,c,a}

我的问题是如何检查是否有五个 x 具有相同的值?

这是我正在开发的扑克应用程序。

I have seven words in the array:

string[7] = {x,x,x,x,x,x,x};

the x is generated from another array:

string[4]={a,b,c,d};

that means each x can be either a or b or c or d. It is randomly generated. this could be an example:

string[7]= {a,a,d,a,a,c,a}

my question is how can I check if there are five x which has the same value?

This is for a poker app Im working on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不会写诗 2024-08-30 07:41:56

您可以使用 Linq 查找相等项的最大数量并测试是否为 5 或更多:

int maxCount = s.GroupBy(x => x).Select(x => x.Count()).Max();

You can use Linq to find the largest number of equal items and test if this is 5 or more:

int maxCount = s.GroupBy(x => x).Select(x => x.Count()).Max();
孤蝉 2024-08-30 07:41:56

你可以这样做:

    List<string> values = new List<string> {"a", "a", "d","a", "a", "c", "a"};

    int count = values.FindAll(id => id == "a").Count();

You can do it like this:

    List<string> values = new List<string> {"a", "a", "d","a", "a", "c", "a"};

    int count = values.FindAll(id => id == "a").Count();
酸甜透明夹心 2024-08-30 07:41:56

您可以将相似的项目分组并找到任何组有五个或更多

from word in new [] { "a", "a", "a", "b", "a", "a", "b" }
group word by word into wordGroup
where wordGroup.Count() >= 5
select wordGroup.Key

You can group the similar items and find it any group have five or more

from word in new [] { "a", "a", "a", "b", "a", "a", "b" }
group word by word into wordGroup
where wordGroup.Count() >= 5
select wordGroup.Key
遇到 2024-08-30 07:41:56

对数组进行排序,然后确定如果有五个或五个以上相同的值,则中间的值就是其中之一。数一数有多少个:

Array.Sort(words);
int cnt = 0;
Array.ForEach(words, s => { if (s == words[3]) cnt++; });

Sort the array, after that you are sure that if there are five or more of the same value, the middle value is one of them. Count how many:

Array.Sort(words);
int cnt = 0;
Array.ForEach(words, s => { if (s == words[3]) cnt++; });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文