需要重构深度嵌套代码的帮助
#include <iostream>
using namespace std;
int main()
{
int range = 20;
int totalCombinations = 0;
for(int i=1; i<=range-2; i++)
{
if(range>i)
{
for(int j=1; j<=range-1; j++)
if(j>i)
{
for(int k=1; k<=range-1; k++)
if(k>j)
{
for(int l=1; l<=range-1; l++)
if(l>k)
{
for(int m=1; m<=range-1; m++)
if(m>l)
{
for(int f=1; f<=range; f++)
if(f>m)
{
cout << " " <<i<< " " <<j<< " " <<k<< " " <<l<< " " <<m<< " " <<f;
cin.get(); //pause
totalCombinations++;
}
}
}
}
}
}
}
cout << "TotalCombinations:" << totalCombinations;
}
#include <iostream>
using namespace std;
int main()
{
int range = 20;
int totalCombinations = 0;
for(int i=1; i<=range-2; i++)
{
if(range>i)
{
for(int j=1; j<=range-1; j++)
if(j>i)
{
for(int k=1; k<=range-1; k++)
if(k>j)
{
for(int l=1; l<=range-1; l++)
if(l>k)
{
for(int m=1; m<=range-1; m++)
if(m>l)
{
for(int f=1; f<=range; f++)
if(f>m)
{
cout << " " <<i<< " " <<j<< " " <<k<< " " <<l<< " " <<m<< " " <<f;
cin.get(); //pause
totalCombinations++;
}
}
}
}
}
}
}
cout << "TotalCombinations:" << totalCombinations;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不直接在哦,我的想法是倒退的,但重点是——你可以轻松地重构这是range
处启动i
来避免这个问题呢?for
条件的一部分。不需要额外的条件。为什么不直接在
i
处启动j
呢?...(对其他两个循环重复)
这消除了一半的嵌套。就循环本身而言,我建议对其使用提取方法。
Why not just startOh, I had that backwards, but the point stands -- you can easily refactor this to be part of thei
atrange
and avoid the problem?for
condition. No need for an extra conditional.Why not just start
j
ati
?... (Repeat for the other two loops)
That gets rid of half your nesting. As far as the loops themselves go, I would suggest using Extract Method on them.
您可以做的第一件事是使用
继续
:这将大大减少嵌套,并且在我看来提高可读性。
The first thing you can do is using
continue
:This will greatly reduce nesting and IMO improve readability.
就像重构任何东西一样。你首先要弄清楚什么
代码正在做。在这种情况下,许多测试都是无关紧要的,并且
每个循环基本上都做同样的事情。你已经解决了一个非常
更普遍问题的具体情况(非常草率)。锻炼
该问题的通用算法将导致更干净、更简单的结果
解决方案,并且是一种更通用的解决方案。像这样的:
上面唯一真正值得评论的是计算
calc
中的limit
,这实际上只是一种优化;你可以使用
n
并获得相同的结果(但您会更多地递归)。您会注意到,在原始版本中,结束条件
循环或多或少是任意的:系统地使用
range
会工作,或者你可以计算出我用于
limit
的公式(其中会导致每个循环有不同的结束条件。
另外,我的代码使用了 C 语言中普遍存在的半开区间,
C++。我想一旦你习惯了它们,你就会发现它们很多
更容易推理。
The same way you refactor anything. You first have to figure out what
the code is doing. In this case, many of the tests are irrelevant, and
each of the loops does basically the same thing. You've solved one very
specific case (very sloppily) of a more general problem. Working out
the general algorithm for the problem will result in a cleaner, simpler
solution, and one that is more general. Something like this:
The only thing really worthy of comment in the above is the calculation
of
limit
incalc
, and that's really just an optimization; you coulduse
n
and get the same results (but you'd recurse a bit more).You'll note that in your original versions, the end conditions of the
loops are more or less arbitrary: using
range
systematically wouldwork, or you could work out the formula which I use for
limit
(whichwould result in a different end condition for each loop.
Also, my code uses the half open intervals which are ubiquious in C and
C++. I think that once you get used to them, you'll find them much
easier to reason about.
我的 C++ 很生疏,所以让我给你一个 C# 的例子。任意数量的嵌套循环都可以替换为一个,如下所示:
My C++ is rusty, so let me give you a C# example. Any number of nested loops can be replaced with just one, as follows: