c#数组编写程序,从一副52张的牌中选出4张,然后计算它们的和。A、J、Q和K 分别表示1、11、12和13。程序应该显示得到和24的选牌次数。
时间: 2024-10-29 21:17:10 浏览: 11
在C#中,你可以使用List<T>或者Array来存储牌,并通过遍历和条件判断来找到满足和为24的四张牌。以下是一个简单的示例程序,使用List来模拟一副扑克牌并计算和为24的组合数:
```csharp
using System;
using System.Collections.Generic;
class Card
{
public int Value { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Card> deck = new List<Card>();
for (int i = 1; i <= 13; i++)
{
// A-K的值分别为1, 11, 12, 13
if (i <= 10)
deck.Add(new Card { Value = i });
else
deck.Add(new Card { Value = 10 + (i % 10) }); // 对于J、Q、K分别加10
}
int targetSum = 24;
int count = 0;
foreach (var card in deck)
{
// 遍历每一张牌
foreach (var secondCard in deck)
{
foreach (var thirdCard in deck)
{
foreach (var fourthCard in deck)
{
// 检查剩余三张牌的和是否等于目标和
if (card.Value + secondCard.Value + thirdCard.Value + fourthCard.Value == targetSum &&
card != secondCard && card != thirdCard && card != fourthCard &&
secondCard != thirdCard && secondCard != fourthCard &&
thirdCard != fourthCard)
{
count++;
break;
}
}
}
}
}
Console.WriteLine($"和为{targetSum}的选牌组合共有 {count} 种。");
阅读全文