|
发表于 16-2-2011 03:25 PM
|
显示全部楼层
if x=0,y=0,z=1
what is the value of x, y, z???? after executing the following code.
if (z=x< || >=&&z==1)
if (z&&y)
y=1;
else
x=1;
yipei 发表于 16-2-2011 01:05 PM
z=x< || >=
你确定 "or" command 可以这样写吗 ??? 我觉得 program 会读不明白吧 ??有error |
|
|
|
|
|
|
|
发表于 16-2-2011 03:31 PM
|
显示全部楼层
If x=4, y=0 and z=2
找 value of x,y,z after executing the following code:
if (z=0 || x && !y)
if (!z)
y=1;
else
x=2
yipei 发表于 16-2-2011 01:11 PM
我试试解答,老实说我真的忘记很多东西了。。。。
if (z=0 || x && !y) -> if z = 0 OR x==not y
since x==not y is true, hence the condition is satisfied, we will enter the algorithm.
if(!z) -> if z not 0 (this is observed from the condition if (z=0 || x && !y) since we previous define z=0,
since z not 0 satisfied, we will enter the algorithm further.
y=1
所以到最后,x 没有变, y 变了,z 没有变。
x=4
y=1
z=2
不知道我这答案对吗 ?? |
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 04:19 PM
|
显示全部楼层
回复 20# JamesTea
我也觉得你的答案有错, suppose y=3, otherwise 你 z 的答案不会是 18 而是 24 。。。你确定你没有抄错答案 ?
w=4,x=5,y=2,z=1 initially has already told you that it will not go through the "if" condition since y is not <=0.
So we look at the "else" algorithm...
z=y*--x ->z=y*(x-1=x)=2*4 = 8
x+=2 -> x=x+2 = 4+2 = 6
++y -> y+1=y = 3
w=x*y++ -> w=x*(y=y+1)=6*4 = 24
lecturer 给的答案是 z=8, x=6, y=4, w=18
这是 last sem 的 test 1, 我借别人的来复印的,她的答案跟你算到的一样,但 y 和 w 却错,算法跟你的一样。。。。 我就是找不到为何 y=4, w=18 而 不是 y=3, w=24 |
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 04:24 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 04:32 PM
|
显示全部楼层
回复 22# JamesTea
我试试解答,老实说我真的忘记很多东西了。。。。
if (z=0 || x && !y) -> if z = 0 OR x==not y
since x==not y is true, hence the condition is satisfied, we will enter the algorithm.
if(!z) -> if z not 0 (this is observed from the condition if (z=0 || x && !y) since we previous define z=0,
since z not 0 satisfied, we will enter the algorithm further.
y=1
所以到最后,x 没有变, y 变了,z 没有变.
x=4
y=1
z=2
不知道我这答案对吗 ??
这题我刚刚才知道有个人的 senior 有给他这份 test, 他的 答案是 x=2, y=0,z=2
但是 senior 没写 step 出来,都不知怎么算出来的,这题也只有 1 分。。。
if (z=0 || x && !y) -> if z = 0 OR x==not y
为什么 x && !y 变成 x==not y ? && mean == ? |
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 04:39 PM
|
显示全部楼层
回复 22# JamesTea
if (z=0 || x && !y)
if (!z)
y=1;
else
x=2
那个 else 是for inner if 的对吗?如果第一个 if true 就没有任何 calculation 对吗? |
|
|
|
|
|
|
|
发表于 16-2-2011 05:36 PM
|
显示全部楼层
回复 JamesTea
那个 else 是for inner if 的对吗?如果第一个 if true 就没有任何 calcul ...
yipei 发表于 16-2-2011 04:39 PM
不是。是 for outer part de... algorithm 的看法是对称的。。 同一个 alignment 的 algorithm 是属于同一个 command . |
|
|
|
|
|
|
|
发表于 16-2-2011 05:39 PM
|
显示全部楼层
回复 JamesTea
这题我刚刚才知道有个人的 senior 有给他这份 test, 他的 答案是 x=2, y=0,z=2
...
yipei 发表于 16-2-2011 04:32 PM
对啊, && 和 == 是一样 就是 two conditions must be satisfied。。。。 AND |
|
|
|
|
|
|
|
发表于 16-2-2011 05:40 PM
|
显示全部楼层
照样子看来我真的 Lost 了 programming 的 knowledge |
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 05:54 PM
|
显示全部楼层
回复 27# JamesTea
不是。是 for outer part de... algorithm 的看法是对称的。。 同一个 alignment 的 algorithm 是属于同一个 command .
不是很明白。。。。怎么说是对称? 什么是 alignment? 什么是 algorithm? |
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 06:00 PM
|
显示全部楼层
回复 28# JamesTea
对啊, && 和 == 是一样 就是 two conditions must be satisfied。。。。 AND
&& 和 == 同样 meaning? 但书上没写到这个哦。。。。
那么 z=x< || >= && z==1
z=x< 是什么意思? || >= 又是什么意思?
书上只是讲到关于 x+=10 是 x=x+10
那么如果 x>=10 可不可以是 x>=10 or x>10 呢?
很 confuse 以下。。。。 |
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 06:01 PM
|
显示全部楼层
回复 29# JamesTea
照样子看来我真的 Lost 了 programming 的 knowledge
你的 programming knowledge 还在,你可是我的 好老师! |
|
|
|
|
|
|
|
楼主 |
发表于 16-2-2011 11:10 PM
|
显示全部楼层
回复 20# JamesTea
我也觉得你的答案有错, suppose y=3, otherwise 你 z 的答案不会是 18 而是 24 。。。你确定你没有抄错答案 ?
w=4,x=5,y=2,z=1 initially has already told you that it will not go through the "if" condition since y is not <=0.
So we look at the "else" algorithm...
z=y*--x ->z=y*(x-1=x)=2*4 = 8
x+=2 -> x=x+2 = 4+2 = 6
++y -> y+1=y = 3
w=x*y++ -> w=x*(y=y+1)=6*4 = 24
我 key in 进 C++,也是拿到的答案是:
z=8
x=6
y=4
w=18 |
|
|
|
|
|
|
|
发表于 16-2-2011 11:44 PM
|
显示全部楼层
回复 JamesTea
&& 和 == 同样 meaning? 但书上没写到这个哦。。。。
那么 z=x< || >= && z==1
z=x< 是什么意思? || >= 又是什么意思?
书上只是讲到关于 x+=10 是 x=x+10
那么如果 x>=10 可不可以是 x>=10 or x>10 呢?
很 confuse 以下。。。。
yipei 发表于 16-2-2011 06:00 PM
对,这两个是一样的 command 来的。 书里面应该有注明的,我也是从课本知道的。
x>=10 当然是指 x 可以等于或大过 10。 x>10 就只能 x 大过10 而已 |
|
|
|
|
|
|
|
发表于 16-2-2011 11:52 PM
|
显示全部楼层
回复 JamesTea
不是很明白。。。。怎么说是对称? 什么是 alignment? 什么是 algorithm?
yipei 发表于 16-2-2011 05:54 PM
我的意思是例如:
if (.......)
if (.......)
else
因为 else 是和第一个 if 对称在同一个 alignment 所以那个 else 是个第一个 if 的。。。只是据我所知,不过如果有错就俄不懂啦 |
|
|
|
|
|
|
|
楼主 |
发表于 17-2-2011 12:55 AM
|
显示全部楼层
回复 34# JamesTea
&& 和 == 同样 meaning? 但书上没写到这个哦。。。。
那么 z=x< || >= && z==1
z=x< 是什么意思? || >= 又是什么意思?
书上只是讲到关于 x+=10 是 x=x+10
那么如果 x>=10 可不可以是 x>=10 or x>10 呢?
很 confuse 以下。。。。
yipei 发表于 16-2-2011 06:00 PM
对,这两个是一样的 command 来的。 书里面应该有注明的,我也是从课本知道的。
x>=10 当然是指 x 可以等于或大过 10。 x>10 就只能 x 大过10 而已
那么 z=x< 又是什么意思呢? z=x< || > 呢? |
|
|
|
|
|
|
|
发表于 17-2-2011 08:53 AM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 17-2-2011 12:17 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 17-2-2011 12:28 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 17-2-2011 04:46 PM
|
显示全部楼层
回复 JamesTea
如果 statement A true, 就 statement B? 如果 B true, 就 statement C? 如果 ...
yipei 发表于 17-2-2011 12:28 PM
错了,if else 只能二选一。。。 statement A true 就进 statement B.... Statement B true then go into Statement C.
Statement C false means end of program 没有了。。 一旦已开始你进入 if 的 algorithm 后就只是在 if 里面跑罢了。。。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|