C# 控制台应用程序,可以找到一些数字的每个组合,但只有在它们可以除以 3 时才写入
C# console application that can find every combination of some numbers but only writes if they can divide by 3
我必须执行一项任务。请帮助我。 © 想要一个 c sharp 控制台应用程序,它可以找到一些数字的每个组合,但只在它们可以被 3 除时写入。
我们一袋里有 15 张卡片。我们将随机抽取 3 张卡片(不更换)。之后我们将添加它们(card1+card2+card3),如果它们的结果可以除以 3,那么我们将它们写入控制台。[(card1+card2+card3)/3]=0
不知道从哪里开始。非常感谢任何帮助!
给定 3 个数字,您可以使用取模运算符判断它们是否可以被 3 整除:
List<int> numbers = new List<int> {4, 8, 11}; // Represents three random cards
// This will be set to true if the sum of the numbers is evenly divisible by 3
bool numbersAreDivisibleByThree = numbers.Sum() % 3 == 0;
这是一个如何使用它的例子:
private static void Main()
{
var cardBag = new List<int>();
var drawnCards = new List<int>();
// Add 15 numbers to the cardBag
for (int i = 1; i <= 15; i++)
{
cardBag.Add(i);
}
// Draw 3 cards at random
var rnd = new Random();
while (drawnCards.Count < 3)
{
var candidateCard = cardBag[rnd.Next(15)];
// In this implementation, we only add unique cards
if (!drawnCards.Contains(candidateCard))
drawnCards.Add(candidateCard);
}
// This will be set to true if the sum of the numbers is evenly divisible by 3
bool numbersAreDivisibleByThree = drawnCards.Sum() % 3 == 0;
// Output results to console
Console.WriteLine("The three random cards drawn from the deck are: {0}",
string.Join(", ", drawnCards));
Console.WriteLine("The sum of the cards is: {0}", drawnCards.Sum());
Console.WriteLine("Is the sum of the cards evenly divisible by three? {0}.",
numbersAreDivisibleByThree);
}
我想你可以从中得到一些启发。
public List<int[]> getCombinations(int[] inArray)
{
List<int[]> outList = new List<int[]>();
for (int i = 0; i < inArray.Length; i++)
for (int j = i + 1; j < inArray.Length; j++)
for (int k = j + 1; k < inArray.Length; k++)
{
int[] outCombination = new int[] { inArray[i], inArray[j], inArray[k] };
outList.Add(outCombination);
}
return outList;
}
static void Main(string[] args)
{
int[] inArray = new int[] { 0, 1, 2, 3, 4, 5 };
// Returned list of combinations...
Program ns = new Program();
List<int[]> Combinations = ns.getCombinations(inArray);
// example: Displaying the results...
foreach (int[] outArray in Combinations)
{
Console.Write(outArray[0] + "," + outArray[1] + "," + outArray[2]);
}
}
既然你看起来像个菜鸟,这里有一个简单的查看方法(虽然不是最有效和成本最低的):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RandomGeneratorPractice
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int randomNumber1 = random.Next(1, 15);
int randomNumber2 = random.Next(1, 15);
int randomNumber3 = random.Next(1, 15);
bool bln = true;
while(bln)
{
if (randomNumber1 == randomNumber2)
{
randomNumber2 = random.Next(1, 15);
bln = true;
}
else if (randomNumber2 == randomNumber3 || randomNumber1==randomNumber3)
{
randomNumber3 = random.Next(1,15);
bln = true;
}
else if ((randomNumber1 != randomNumber2) && (randomNumber1 != randomNumber3) && (randomNumber2 != randomNumber3))
{
bln = false;
}
}
int dividend = randomNumber1 + randomNumber2 + randomNumber3;
double divisor = 3;
double quotient = (randomNumber1 + randomNumber2 + randomNumber3) / 3;
Console.WriteLine("(" + randomNumber1 + "+" + randomNumber2 + "+" + randomNumber3 + ") / 3 = " + (dividend / divisor));
if (dividend % divisor == 0)
{
Console.WriteLine("You CAN divide by 3 evenly");
}
else
{
Console.WriteLine("You CANNOT divide by 3 evenly");
}
Console.WriteLine("Press ENTER to exit...");
Console.Read();
}
}
}
我必须执行一项任务。请帮助我。 © 想要一个 c sharp 控制台应用程序,它可以找到一些数字的每个组合,但只在它们可以被 3 除时写入。
我们一袋里有 15 张卡片。我们将随机抽取 3 张卡片(不更换)。之后我们将添加它们(card1+card2+card3),如果它们的结果可以除以 3,那么我们将它们写入控制台。[(card1+card2+card3)/3]=0
不知道从哪里开始。非常感谢任何帮助!
给定 3 个数字,您可以使用取模运算符判断它们是否可以被 3 整除:
List<int> numbers = new List<int> {4, 8, 11}; // Represents three random cards
// This will be set to true if the sum of the numbers is evenly divisible by 3
bool numbersAreDivisibleByThree = numbers.Sum() % 3 == 0;
这是一个如何使用它的例子:
private static void Main()
{
var cardBag = new List<int>();
var drawnCards = new List<int>();
// Add 15 numbers to the cardBag
for (int i = 1; i <= 15; i++)
{
cardBag.Add(i);
}
// Draw 3 cards at random
var rnd = new Random();
while (drawnCards.Count < 3)
{
var candidateCard = cardBag[rnd.Next(15)];
// In this implementation, we only add unique cards
if (!drawnCards.Contains(candidateCard))
drawnCards.Add(candidateCard);
}
// This will be set to true if the sum of the numbers is evenly divisible by 3
bool numbersAreDivisibleByThree = drawnCards.Sum() % 3 == 0;
// Output results to console
Console.WriteLine("The three random cards drawn from the deck are: {0}",
string.Join(", ", drawnCards));
Console.WriteLine("The sum of the cards is: {0}", drawnCards.Sum());
Console.WriteLine("Is the sum of the cards evenly divisible by three? {0}.",
numbersAreDivisibleByThree);
}
我想你可以从中得到一些启发。
public List<int[]> getCombinations(int[] inArray)
{
List<int[]> outList = new List<int[]>();
for (int i = 0; i < inArray.Length; i++)
for (int j = i + 1; j < inArray.Length; j++)
for (int k = j + 1; k < inArray.Length; k++)
{
int[] outCombination = new int[] { inArray[i], inArray[j], inArray[k] };
outList.Add(outCombination);
}
return outList;
}
static void Main(string[] args)
{
int[] inArray = new int[] { 0, 1, 2, 3, 4, 5 };
// Returned list of combinations...
Program ns = new Program();
List<int[]> Combinations = ns.getCombinations(inArray);
// example: Displaying the results...
foreach (int[] outArray in Combinations)
{
Console.Write(outArray[0] + "," + outArray[1] + "," + outArray[2]);
}
}
既然你看起来像个菜鸟,这里有一个简单的查看方法(虽然不是最有效和成本最低的):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RandomGeneratorPractice
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int randomNumber1 = random.Next(1, 15);
int randomNumber2 = random.Next(1, 15);
int randomNumber3 = random.Next(1, 15);
bool bln = true;
while(bln)
{
if (randomNumber1 == randomNumber2)
{
randomNumber2 = random.Next(1, 15);
bln = true;
}
else if (randomNumber2 == randomNumber3 || randomNumber1==randomNumber3)
{
randomNumber3 = random.Next(1,15);
bln = true;
}
else if ((randomNumber1 != randomNumber2) && (randomNumber1 != randomNumber3) && (randomNumber2 != randomNumber3))
{
bln = false;
}
}
int dividend = randomNumber1 + randomNumber2 + randomNumber3;
double divisor = 3;
double quotient = (randomNumber1 + randomNumber2 + randomNumber3) / 3;
Console.WriteLine("(" + randomNumber1 + "+" + randomNumber2 + "+" + randomNumber3 + ") / 3 = " + (dividend / divisor));
if (dividend % divisor == 0)
{
Console.WriteLine("You CAN divide by 3 evenly");
}
else
{
Console.WriteLine("You CANNOT divide by 3 evenly");
}
Console.WriteLine("Press ENTER to exit...");
Console.Read();
}
}
}