C 编程同时

发布于 2024-12-07 07:41:03 字数 255 浏览 0 评论 0原文

#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])  
{   
    int x=0;
    int y=0;
    while (x<15)y++,x+=++y;
    printf ("%i %i",x, y);
    getchar ();
    getchar ();
    return 0;
}

我不知道为什么最后x是20而y是8。 请一步步解释。

#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])  
{   
    int x=0;
    int y=0;
    while (x<15)y++,x+=++y;
    printf ("%i %i",x, y);
    getchar ();
    getchar ();
    return 0;
}

I don't know why x is 20 and y is 8 at the end.
Please explain it step by step.

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

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

发布评论

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

评论(8

荒岛晴空 2024-12-14 07:41:03
while (x<15)y++,x+=++y;

=>

while (x<15) {

    y++;
    x += ++y;

}

=>

while (x < 15) {
    y += 2;
    x += y;
}

因此:

Before 1st iteration: x = 0, y = 0;

After 1st iteration: x = 2, y = 2;
After 2nd iteration: x = 6, y = 4;
After 3rd iteration: x = 12, y = 6;
After 4th iteration: x = 20, y = 8;

请注意,这些值也有一个简单的封闭公式:x = n*n - ny = 2*n

while (x<15)y++,x+=++y;

=>

while (x<15) {

    y++;
    x += ++y;

}

=>

while (x < 15) {
    y += 2;
    x += y;
}

So:

Before 1st iteration: x = 0, y = 0;

After 1st iteration: x = 2, y = 2;
After 2nd iteration: x = 6, y = 4;
After 3rd iteration: x = 12, y = 6;
After 4th iteration: x = 20, y = 8;

Note that there is a simple closed formula for these values as well: x = n*n - n and y = 2*n.

哑剧 2024-12-14 07:41:03

请记住:

  1. y++ 递增 y
  2. x+=++y 首先递增 y,然后将其添加到 x

这给出了 x 和 y 的以下值:

iterations   x    y
0            0    0
1            2    2
2            6    4
3            12   6
4            20   8

Remember that :

  1. y++ increments y
  2. x+=++y first increments y and then adds it to x

Which gives the following values for x and y :

iterations   x    y
0            0    0
1            2    2
2            6    4
3            12   6
4            20   8
苏璃陌 2024-12-14 07:41:03

x<15-> y=1,x=0+(y=2)=2

2 15 → y=3,x=2+(y=4)=6

6 15 → y=5,x=6+(y=6)=12

12 15 → y=7, x=12+(y=8)=20

完成
x=20, y=8

逗号运算符强制执行顺序。 x,y表示先执行x,然后执行y。

x<15 -> y=1, x=0+(y=2)=2

2<15 -> y=3, x=2+(y=4)=6

6<15 -> y=5, x=6+(y=6)=12

12<15 -> y=7, x=12+(y=8)=20

Done
x=20, y=8

The comma operator enforces the order of execution. x,y means that x is executed first, and then y.

属性 2024-12-14 07:41:03

如果您遵循变量将会发生的情况:

First Loop:
x=0, y=0
y++ => y=1
x+=++y => x=2, y=2

Second Loop:
x=1, y=2
y++ => y=3
x+=++y => x=6, y=4

Third Loop:
y++ => y=5
x+=++y => x=12, y=6

Fourth Loop:
y++ => y=7
x+=++y => x=20, y=8

while 循环将退出。

If you follow what is going to happen to your variables :

First Loop:
x=0, y=0
y++ => y=1
x+=++y => x=2, y=2

Second Loop:
x=1, y=2
y++ => y=3
x+=++y => x=6, y=4

Third Loop:
y++ => y=5
x+=++y => x=12, y=6

Fourth Loop:
y++ => y=7
x+=++y => x=20, y=8

And while loop will exit.

说好的呢 2024-12-14 07:41:03

我把它改成了

#include <stdio.h>
int main()
{
    int x=0;
    int y=0;
    while (x<15) {
        y++,x+=++y;
        printf ("%i %i\n",x, y);
    }
    return 0;
}

现在就可以了

H:\Temp>a.exe
2 2
6 4
12 6
20 8

为什么?因为 y 在每一步中都会增加两次,并且 x 会添加新值。所以你基本上得到 2+4+6+8 = 20。

但我不确定这是定义的行为。如果 , 运算符定义了一个序列点。

I changed it to

#include <stdio.h>
int main()
{
    int x=0;
    int y=0;
    while (x<15) {
        y++,x+=++y;
        printf ("%i %i\n",x, y);
    }
    return 0;
}

and it yields

H:\Temp>a.exe
2 2
6 4
12 6
20 8

now.

Why? Because y gets incremented twice in each step, and x gets added the new value. So you essentially get 2+4+6+8 = 20.

But I'm not sure it is defined behaviour. It is if the , operator defines a sequence point.

私藏温柔 2024-12-14 07:41:03

让我们看一下循环(循环后的条件):

  1. x=2, y=2
  2. x=6, y=4
  3. x=12, y=6
  4. x=20, y=8

在每个循环中,有效完成的是:

y+=2
x+=y

这会导致上面写的状态。

Let's go through the loops (conditions after the loop):

  1. x=2, y=2
  2. x=6, y=4
  3. x=12, y=6
  4. x=20, y=8

In each loop, what effectively is done is:

y+=2
x+=y

which results in the states written above.

俏︾媚 2024-12-14 07:41:03

好吧,这就是 while 子句中每个循环发生的情况:

  • 将 y 加一
  • 将 y 加一
  • 则将y 加到 x 上
  • 如果 x >= 15,

停止循环现在用数字表示,就在循环结束之前:

  1. x = 0, y=0
  2. x=2, y=2
  3. x=6, y=4
  4. x=12, y=6
  5. x=20, y=8 --> x> 15 -->完成循环。

Alright, this is what is happening every loop in your while clause:

  • Increment y by one
  • Increment y by one additionally
  • add y to x
  • if x >= 15 stop the loop

Now in numbers, right before the end of the loop:

  1. x = 0, y = 0
  2. x = 2, y = 2
  3. x = 6, y = 4
  4. x = 12, y = 6
  5. x = 20, y = 8 --> x > 15 --> finish the loop.
不爱素颜 2024-12-14 07:41:03

在每次迭代中

y increased by 2 ( y +=2 ) ,
after that x increased by x + y ( x= x+y)
it skips when x = 20 ( as x is now > 15  ) 

in every iteration

y increased by 2 ( y +=2 ) ,
after that x increased by x + y ( x= x+y)
it skips when x = 20 ( as x is now > 15  ) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文