如何在两个值之间递增和递减

发布于 2024-10-11 05:30:44 字数 273 浏览 7 评论 0原文

这很尴尬,但是:

假设我想将 x 加 1,直到它达到 100。此时我想从 x 中减去 1,直到它达到 1。然后我想将 x 加 1,直到它达到 100,并且很快。

有人可以为这个让我感觉特别愚蠢的问题提供一些简单的伪代码吗?

谢谢:)


编辑 1

抱歉!我的例子太简单了。实际上,我将在每次迭代中使用随机数进行递增,因此需要 (x == 100) 的响应将不起作用,因为 x 肯定会高于 100 并低于 1。

This is embarrassing, but:

Let say I want to add 1 to x until it reaches 100. At that point I then want to subtract 1 from x until it reaches 1. Then I want to add 1 to x until it reaches 100, and so on.

Could someone provide some simple pseudocode to this question that is making me feel especially dumb.

Thanks :)


EDIT 1

Apologies! I made my example too simple. I actually will be incrementing using a random number at each iteration, so responses that require (x == 100) would not work as x will surely go above 100 and below 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

缪败 2024-10-18 05:30:44

这是数学方法:

for(int i=0;i<10000000;i++)
  print(abs(i%200-100))

算法的方法:

int i = 1;
while(1)
{
 while(i<100)print(i++);
 while(i>1)print(--i);
}

随机更新:

int i = 1;
while(1)
{
 while(i<100)print(i=min(100,i+random()));
 while(i>1)print(i=max(1,i-random()));
}

Here is math way:

for(int i=0;i<10000000;i++)
  print(abs(i%200-100))

Algo's way:

int i = 1;
while(1)
{
 while(i<100)print(i++);
 while(i>1)print(--i);
}

Random updated:

int i = 1;
while(1)
{
 while(i<100)print(i=min(100,i+random()));
 while(i>1)print(i=max(1,i-random()));
}
孤单情人 2024-10-18 05:30:44
int ceiling = 100;
int floor = 1;
int x = 1;
int step = GetRandomNumber(); //assume this isn't 0

while(someArbitraryCuttoffOrAlwaysTrueIDK) {
    while(x + step <= ceiling) {
        x += step;
    }
    while(x - step >= floor) {
        x -= step;
    }
}

或者,更简洁(冒着不太清晰的风险):

while(someArbitraryCuttoffOrAlwaysTrueIDK) {
    while((step > 0  && x + step <= ceiling) || (step < 0 && x + step >= floor)) 
    {
        x += step;
    }
    step = step * -1;
}

或者:

while(someArbitraryCuttoffOrAlwaysTrueIDK) {
    if((step > 0  && x + step > ceiling) || (step < 0 && x + step < floor)) 
    {
        step = step * -1;
    }
    x += step;
}
int ceiling = 100;
int floor = 1;
int x = 1;
int step = GetRandomNumber(); //assume this isn't 0

while(someArbitraryCuttoffOrAlwaysTrueIDK) {
    while(x + step <= ceiling) {
        x += step;
    }
    while(x - step >= floor) {
        x -= step;
    }
}

Or, being more concise (at the risk of being less clear):

while(someArbitraryCuttoffOrAlwaysTrueIDK) {
    while((step > 0  && x + step <= ceiling) || (step < 0 && x + step >= floor)) 
    {
        x += step;
    }
    step = step * -1;
}

Alternatively:

while(someArbitraryCuttoffOrAlwaysTrueIDK) {
    if((step > 0  && x + step > ceiling) || (step < 0 && x + step < floor)) 
    {
        step = step * -1;
    }
    x += step;
}
北陌 2024-10-18 05:30:44

C#:

Random rnd = new Random();
int someVarToIncreaseDecrease = 0;
bool increasing = true;

while(true) {
    int addSubtractInt = rnd.Next(someUpperBound);

    if (increasing && (someVarToIncreaseDecrease + addSubtractInt >= 100)) 
       increasing = false;
    else if (!increasing && (someVarToIncreaseDecrease - addSubtractInt < 0)) 
       increasing = true;

    if (increasing) {
       someVarToIncreaseDecrease += addSubtractInt;
    }
    else {
       someVarToIncreaseDecrease -= addSubtractInt;
    }
}

C#:

Random rnd = new Random();
int someVarToIncreaseDecrease = 0;
bool increasing = true;

while(true) {
    int addSubtractInt = rnd.Next(someUpperBound);

    if (increasing && (someVarToIncreaseDecrease + addSubtractInt >= 100)) 
       increasing = false;
    else if (!increasing && (someVarToIncreaseDecrease - addSubtractInt < 0)) 
       increasing = true;

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