请帮我写这个简单的、基本的伪代码
这对你们来说应该很容易,但我仍然无法在脑海中形象化。这就是我想要做的:
if (flag)
for (i = 0; i < 10; i++)
{LARGE_BLOCK_OF_CODE (that visits an array in order)}
else
for (i = 9; i >= 0; i--)
{LARGE_BLOCK_OF_CODE (that visits an array in REVERSE order)}
问题是这段代码不能放在函数中,而且我不想复制/粘贴它两次。有什么办法可以用这种方式做到这一点:
if (flag)
loop = for (i = 0; i < 10; i++);
else
loop = for (i = 9; i >= 0; i--);
loop
{LARGE_BLOCK_OF_CODE (that visits an array in the order specified in 'loop')}
??
This should be very easy for you guys, but I still can't get it visualized in my mind. Here's what I'm trying to do:
if (flag)
for (i = 0; i < 10; i++)
{LARGE_BLOCK_OF_CODE (that visits an array in order)}
else
for (i = 9; i >= 0; i--)
{LARGE_BLOCK_OF_CODE (that visits an array in REVERSE order)}
The problem is that this block of code cannot be placed in a function, and I don't want to copy/paste it twice. Is there any way I can do that in this manner:
if (flag)
loop = for (i = 0; i < 10; i++);
else
loop = for (i = 9; i >= 0; i--);
loop
{LARGE_BLOCK_OF_CODE (that visits an array in the order specified in 'loop')}
??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为循环的开始和结束创建变量,并创建第三个变量来确定迭代的“方向”。
然后让循环运行您的一个代码块。像这样...
Create variables for the start and end of the loop, and create a third variable to determine the 'direction' for the iteration.
Then just let the loop run through your ONE block of code. Like this...
将 LARGE_BLOCK_OF_CODE 放入函数中,接收顺序作为参数。然后在您需要的任意数量的循环中重复使用它。
Put that LARGE_BLOCK_OF_CODE in a function, receiving order as a parameter. Then reuse it in any number of loops you need.
不知道你的数组有多大。也许可以生成一个附加数组,以所需的顺序保存所需的索引:
I don't know how big your array is. Maybe it is possible to generate an additional array that holds the required indexes in the required order:
好吧,我知道...:p 那太多了。
是的,FlipScript 的解决方案更好,尽管它可能看起来不太整洁。 :)
Well I know...:p thats too many if else.
And yes, FlipScript's solution is better though it might not look neat. :)