C# 和 SQL 难题的提示
我正在浏览 SO 职业,发现一份工作有一份 pdf 文件,其中包含一些他们希望申请人发送的谜题。
虽然我对这份工作不感兴趣,但我还是阅读了这些问题,并在 Visual Studio / SMSS 中进行了尝试。第一个问题相当容易解决,尽管我想不出是否有任何方法可以优化它(我用 C# 解决了它)。第二个谜题只有一个明显的解决方案让我印象深刻,我想不出任何其他解决方案。
我不确定在这里讨论这些问题是否不好,但如果有人能给我一些提示或者建议我可以在不造成任何悲伤的情况下问这个问题的地方,我将不胜感激。
问题在这里:http://www.debtx.com/doc/DebtX_Programming_Problems.pdf< /a>
我可以让第一个问题滑过去,但第二个问题却让我陷入了除明显问题之外的其他解决方法上。遗憾的是,SO 上没有 PM 功能...
第一部分的样板解决方案 C#:
public static bool Compare(int[] num, int[] div)
{
for (int i = 0; i < num.Length; i++)
{
for (int j = 0; j < div.Length; j++)
{
if (num[i] % div[j] == 0)
return true;
}
}
return false;
}
我的 SQL 解决方案
select Table1.Key1, Table1.Key2 from Table1 inner join Table2 on Table1.Key1 = Table2.key2 where IsDeleted=0
select * from Table1 where key1 in(select Key2 from Table2 where IsDeleted=0)
虽然一切看起来都很相似
I was browsing SO careers and came across a job that had a pdf with a couple of puzzles they wanted applicants to send in.
Although I'm not interested in the job, I read the questions anyway and had a play in Visual Studio / SMSS. The first question was fairly easy to solve although I couldn't think if any way to optimise it (I solved it in C#). The second puzzle only one obvious solution strikes me and I can't think of any others.
I'm not sure if it's bad to discuss these questions here though, but if anyone can give me some hints or perhaps suggest somewhere where I can ask this without creating any grief it'd be appreciated.
The questions are in here: http://www.debtx.com/doc/DebtX_Programming_Problems.pdf
I could let the first one slide but the second one has me stumped on other ways of solving it than the obvious. Shame there's no PM function on SO...
Boilerplate solution for the first part C#:
public static bool Compare(int[] num, int[] div)
{
for (int i = 0; i < num.Length; i++)
{
for (int j = 0; j < div.Length; j++)
{
if (num[i] % div[j] == 0)
return true;
}
}
return false;
}
My SQL Solutions
select Table1.Key1, Table1.Key2 from Table1 inner join Table2 on Table1.Key1 = Table2.key2 where IsDeleted=0
select * from Table1 where key1 in(select Key2 from Table2 where IsDeleted=0)
It all seems so samey though
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
几个使用伪 SQL 的示例,以免泄露太多
Not In
Not Exists
Outter Join Is Null
couple of examples using pseudo SQL to not give too much away
Not In
Not Exists
Outter Join Is Null
C# 问题的一项优化是对 DIV 数组进行排序。从较小的数字开始,您更有可能快速找到匹配项。
编辑:对 C# 问题的另一个优化可能是查看类似于素数的方法埃拉托斯特尼筛< /a> 一般理论是,您可以抢占一些结果,而无需执行检查。
至于 SQL 问题,正如其他人所说,三种明显(常见)的方式是 JOIN、IN 和 EXISTS。
One optimisation to the C# question is to sort the DIV array. You're more likely to find a match quickly starting with the smaller numbers.
EDIT: Another optimisation to the C# question may be to look at an approach similar to the Prime Number Sieve of Eratosthenes the general theory being that you pre-empt some results without having to perform the checks.
As for the SQL question, the three obvious (common) ways are as others have stated, a JOIN, an IN and an EXISTS.
那么您已经使用过哪种解决方案?我立即认为可以使用带有
IN
的子查询、使用LEFT OUTER JOIN
并过滤NULL
或使用EXISTS 来完成
。Well which solution have you used already? Immediately I think it can be done using a subquery with
IN
, using aLEFT OUTER JOIN
and filtering onNULL
, or usingEXISTS
.剧透警告!!!!
Spoiler alert!!!!!