请帮我写这个简单的、基本的伪代码

发布于 2024-10-07 05:23:46 字数 552 浏览 1 评论 0原文

这对你们来说应该很容易,但我仍然无法在脑海中形象化。这就是我想要做的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我们的影子 2024-10-14 05:23:46

为循环的开始和结束创建变量,并创建第三个变量来确定迭代的“方向”。

然后让循环运行您的一个代码块。像这样...

int iStart = 0;
int iEnd = 9;
int iIncrement = +1;

if (reverse)
{
    iStart = 9;
    iEnd = 0;
    iIncrement = -1;
}

for (i = iStart; i != iEnd; i += iIncrement)
{
    //...large block of code here...
}

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...

int iStart = 0;
int iEnd = 9;
int iIncrement = +1;

if (reverse)
{
    iStart = 9;
    iEnd = 0;
    iIncrement = -1;
}

for (i = iStart; i != iEnd; i += iIncrement)
{
    //...large block of code here...
}
段念尘 2024-10-14 05:23:46

将 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.

冷弦 2024-10-14 05:23:46

不知道你的数组有多大。也许可以生成一个附加数组,以所需的顺序保存所需的索引:

indexes = []

for (i = 0; i < 10; i++)
  indexes.add(i)

if (!flag)
  indexes.reverse()

for each (i in indexes)
  {LARGE_BLOCK_OF_CODE}

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:

indexes = []

for (i = 0; i < 10; i++)
  indexes.add(i)

if (!flag)
  indexes.reverse()

for each (i in indexes)
  {LARGE_BLOCK_OF_CODE}
眼角的笑意。 2024-10-14 05:23:46
for (i = flag?0:9; i != flag?10:0; i += flag?1:-1)
{
//LARGE_BLOCK_OF_CODE 
}

好吧,我知道...:p 那太多了。

是的,FlipScript 的解决方案更好,尽管它可能看起来不太整洁。 :)

for (i = flag?0:9; i != flag?10:0; i += flag?1:-1)
{
//LARGE_BLOCK_OF_CODE 
}

Well I know...:p thats too many if else.

And yes, FlipScript's solution is better though it might not look neat. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文