我定义了一个新版本if:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
Run Code Online (Sandbox Code Playgroud)
然后我用它如下:
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
Run Code Online (Sandbox Code Playgroud)
据我所知,由于else-clause传递给new-if在sqrt-iter过程始终评估,sqrt-iter从未停止使自身的递归调用.
但我不明白为什么我们在good-enough?return true=> 时不停止guess
我有以下代码:
if (!string.IsNullOrEmpty(_location.FolderName))
{
name = _location.FolderName + '/' + name;
}
Run Code Online (Sandbox Code Playgroud)
/在两个字符串之间添加是否正确?或者我应该使用以下代码:
if (!string.IsNullOrEmpty(_location.FolderName))
{
name = _location.FolderName + "/" + name;
}
Run Code Online (Sandbox Code Playgroud) 我有非负整数N,我需要在N的排列中找到最大的数字.
例如,给定N = 213函数应该返回321.
如何在O(1)时间和空间复杂性方面做到这一点?
N是[0 ... 2,147,483,647]范围内的整数
如果结果超出100 000 000我们可以返回-1
private void GenerateRecords(JobRequest request)
{
for (var day = 0; day < daysInRange; day++)
{
foreach (var coreId in request.CoreIds)
{
foreach (var agentId in Enumerable.Range(0, request.AgentsCount).Select(x => Guid.NewGuid()))
{
for (var copiesDone = 0; copiesDone < request.CopiesToMake; copiesDone++)
{
foreach (var jobInfoRecord in request.Jobs)
{
foreach (var status in request.Statuses)
{
//DoSomeWork();
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法提高多次迭代的性能?我真的需要所有这些循环,但我想知道如何改进(加速)迭代?也许使用linq?
我想找到方程的所有正整数解,
a^3 + b^3 = c^3 + d^3其中a,b,c,d是1到1000之间的整数;
for (int a = 1; a <= 1000; ++a)
{
for (int b = 1; b <= 1000; ++b)
{
for (int c = 1; c <= 1000; ++c)
{
for (int d = 1; d <= 1000; ++d)
{
if (Math.Pow(a, 3) + Math.Pow(b, 3) == Math.Pow(c, 3) + Math.Pow(d, 3))
{
Console.WriteLine("{0} {1} {2} {3}", a,b,c,d);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道d = Math.Pow(a^3 + b^3 - c^3, 1/3) …
public Data GetData()
{
...
updateCache();
}
public void timer()
{
//I would like to call GetData() every minute
}
Run Code Online (Sandbox Code Playgroud)
我想GetData()每分钟触发一次并更新缓存。
在 C# 中执行此操作的最佳方法是什么?