C# 使用linq解决大脑难题
首先,让我向Marc Grasser、Dahlbyk和其他人表示衷心的感谢,感谢他们帮助我实际应用linq 以下是我在一次面试中遇到的几个问题,这些问题是为了解决申请Linq的问题。因为我不熟悉Linq,所以我没有使用Linq就解决了它 我很感激这些答案,它们帮助我使用Linq解决这些问题 提前谢谢。C# 使用linq解决大脑难题,c#,linq-to-objects,C#,Linq To Objects,首先,让我向Marc Grasser、Dahlbyk和其他人表示衷心的感谢,感谢他们帮助我实际应用linq 以下是我在一次面试中遇到的几个问题,这些问题是为了解决申请Linq的问题。因为我不熟悉Linq,所以我没有使用Linq就解决了它 我很感激这些答案,它们帮助我使用Linq解决这些问题 提前谢谢。 问题1: 问题是找到不同的数字,这样,无论用什么顺序生成一个三位数字,该数字都不能被下列数字整除: 3、5、7、11、13或17 public void FindingRareNumbers()
问题1: 问题是找到不同的数字,这样,无论用什么顺序生成一个三位数字,该数字都不能被下列数字整除: 3、5、7、11、13或17
public void FindingRareNumbers()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
for (int k = 1; k <= 9; k++)
{
//to form the three digit
string digit = i.ToString() + j.ToString() + k.ToString();
//converting to integer
int StrToDigit = Convert.ToInt32(digit);
char[] digitcombination = digit.ToCharArray();
string PossibleCombination = "";
bool testpassed = false;
int dcount = 0;
#region different possible combinations
for (int p = 0; p <= 2; p++)
{
for (int q = 0; q <= 2; q++)
{
for (int r = 0; r <= 2; r++)
{
// The following condition avoid the repeatance
// of digit like 111,111,111
if (p != q && p != r && r != q)
{
PossibleCombination =
digitcombination[p].ToString() +
digitcombination[q].ToString() +
digitcombination[r].ToString();
int num = Convert.ToInt32(PossibleCombination);
if (num % 3 != 0 && num % 5 != 0 && num % 7 != 0
&& num % 11 != 0 && num % 11 != 0
&& num % 13 != 0 && num % 17 != 0)
{
//count is increment for 6 times
// it satisfies the condition
dcount++;
testpassed = true;
}
}
}
}
}
#endregion combination
if (testpassed && dcount==6)
{
Console.WriteLine(StrToDigit);
}
}
}
}
}
为了确保没有埋伏,假设三位数字
是a、b和c。那么,这些数字的组合都不是:
比如abc、acb、bac、bca、cab和cba将除以3、5、7、11、13或17。
例如:
当我取248时,它的任何组合(2844284842824)都不能被3,5,7,11,13或17整除
public void FindingRareNumbers()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
for (int k = 1; k <= 9; k++)
{
//to form the three digit
string digit = i.ToString() + j.ToString() + k.ToString();
//converting to integer
int StrToDigit = Convert.ToInt32(digit);
char[] digitcombination = digit.ToCharArray();
string PossibleCombination = "";
bool testpassed = false;
int dcount = 0;
#region different possible combinations
for (int p = 0; p <= 2; p++)
{
for (int q = 0; q <= 2; q++)
{
for (int r = 0; r <= 2; r++)
{
// The following condition avoid the repeatance
// of digit like 111,111,111
if (p != q && p != r && r != q)
{
PossibleCombination =
digitcombination[p].ToString() +
digitcombination[q].ToString() +
digitcombination[r].ToString();
int num = Convert.ToInt32(PossibleCombination);
if (num % 3 != 0 && num % 5 != 0 && num % 7 != 0
&& num % 11 != 0 && num % 11 != 0
&& num % 13 != 0 && num % 17 != 0)
{
//count is increment for 6 times
// it satisfies the condition
dcount++;
testpassed = true;
}
}
}
}
}
#endregion combination
if (testpassed && dcount==6)
{
Console.WriteLine(StrToDigit);
}
}
}
}
}
例如:
解决方案之一如下:
-----------
2 9 4
-----------
7 5 3
----------
6 1 8
----------
首先:
static IEnumerable<int> Permute(int x, int y, int z)
{
yield return x * 100 + y * 10 + z;
yield return x * 100 + z * 10 + y;
yield return y * 100 + x * 10 + z;
yield return y * 100 + z * 10 + x;
yield return z * 100 + x * 10 + y;
yield return z * 100 + y * 10 + x;
}
static void Main()
{
var divs = new[] {3,5,7,11,13,17};
// combinations of 1-9
var combinations =
from x in Enumerable.Range(1, 7)
from y in Enumerable.Range(x + 1, 8 - x)
from z in Enumerable.Range(y + 1, 9 - y)
select new { x, y, z };
// permute
var qry = from comb in combinations
where !Permute(comb.x, comb.y, comb.z).Any(
i => divs.Any(d => i % d == 0))
select comb;
foreach (var answer in qry)
{
Console.WriteLine("{0}, {1}, {2}", answer.x, answer.y, answer.z);
}
}
首先:
static IEnumerable<int> Permute(int x, int y, int z)
{
yield return x * 100 + y * 10 + z;
yield return x * 100 + z * 10 + y;
yield return y * 100 + x * 10 + z;
yield return y * 100 + z * 10 + x;
yield return z * 100 + x * 10 + y;
yield return z * 100 + y * 10 + x;
}
static void Main()
{
var divs = new[] {3,5,7,11,13,17};
// combinations of 1-9
var combinations =
from x in Enumerable.Range(1, 7)
from y in Enumerable.Range(x + 1, 8 - x)
from z in Enumerable.Range(y + 1, 9 - y)
select new { x, y, z };
// permute
var qry = from comb in combinations
where !Permute(comb.x, comb.y, comb.z).Any(
i => divs.Any(d => i % d == 0))
select comb;
foreach (var answer in qry)
{
Console.WriteLine("{0}, {1}, {2}", answer.x, answer.y, answer.z);
}
}
我同意马克对你的第一个问题的解决方案是合理的。但我认为这里有一个更大的问题,那就是“我如何以一种温和的方式解决这样的问题?” 请注意,您的解决方案是如何完全“程序化”和“强制性”的。您的代码指定了一系列步骤,这些步骤将通过深度循环一个接一个地执行。除非你了解它在更大整体中的位置,否则沿途的每一步都是毫无意义的 在使用LINQ解决问题时,我喜欢使用两种想法:
- 从逻辑上描述程序正在做什么,而不是列出一系列命令
- 将问题描述为针对数据集的查询,而不是要遵循的过程
var query = from c in ThreeDigitCombinations()
where DivisibilityCheckPasses(c)
select c;
foreach(Combination result in query) Console.WriteLine(result);
现在您可以继续进一步分解这些问题,依次使用LINQ解决每个子问题
你的“魔方”问题也是如此;你正在寻找一种具有特定属性的置换,所以写一个置换生成器,写一个过滤器,然后执行它。我同意Marc对你的第一个问题的解决方案是一种合理的方法。但我认为这里有一个更大的问题,那就是“我如何以一种温和的方式解决这样的问题?” 请注意,您的解决方案是如何完全“程序化”和“强制性”的。您的代码指定了一系列步骤,这些步骤将通过深度循环一个接一个地执行。除非你了解它在更大整体中的位置,否则沿途的每一步都是毫无意义的 在使用LINQ解决问题时,我喜欢使用两种想法:
- 从逻辑上描述程序正在做什么,而不是列出一系列命令
- 将问题描述为针对数据集的查询,而不是要遵循的过程
var query = from c in ThreeDigitCombinations()
where DivisibilityCheckPasses(c)
select c;
foreach(Combination result in query) Console.WriteLine(result);
现在您可以继续进一步分解这些问题,依次使用LINQ解决每个子问题
你的“魔方”问题也是如此;您正在寻找具有特定属性的置换,因此请编写置换生成器,编写筛选器,然后执行它。对于两个数字相同的置换如何?例如,你为什么不测试113、131、311呢?那两个数字相同的排列呢?例如,你为什么不测试113131311呢;参见问题中的“确保不存在欺诈”评论;它与非重复排列有关。特别是“例如abc、acb、bac、bca、cab和cba将除以3、5、7、11、13或17。”重新评论;参见问题中的“确保不存在欺诈”评论;它与非重复排列有关。特别是“比如abc、acb、bac、bca、cab和cba将除以3、5、7、11、13或17。”