佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: yipei

会做吗?C

[复制链接]
发表于 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
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
回复 21# JamesTea


    回复 21# JamesTea


  
z=x< || >=  

你确定 "or" command 可以这样写吗 ??? 我觉得 program 会读不明白吧 ??有error


我不确定 “or" 怎么写,但书上的就是两条直线,我在 keyboard 就只找到 || 是直线而已哦。。。。 lab assignment 还没做到这个。。。。 我打进 C++,也是一直有 error, 都不知怎么好。。。。

不然 "or" 要怎么写才没 error?
回复

使用道具 举报

 楼主| 发表于 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 对吗?
回复

使用道具 举报

Follow Us
发表于 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
回复

使用道具 举报


ADVERTISEMENT

发表于 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< || > 呢?
回复

使用道具 举报


ADVERTISEMENT

发表于 17-2-2011 08:53 AM | 显示全部楼层
回复  JamesTea

   



那么 z=x< 又是什么意思呢? z=x< || > 呢?
yipei 发表于 17-2-2011 12:55 AM



所以我就问了,你确定你有用错吗 ??? 因为我 interpret 不到这。。

什么是 z 等于 x 然后小过或大过 ?? 我觉得有 error ...
回复

使用道具 举报

 楼主| 发表于 17-2-2011 12:17 PM | 显示全部楼层
所以我就问了,你确定你有用错吗 ??? 因为我 interpret 不到这。。

什么是 z 等于 x 然后小过或大过 ?? 我觉得有 error ...


我也是一直拿到 error, 这是 pass year 的 题目来的哦。。。。
回复

使用道具 举报

 楼主| 发表于 17-2-2011 12:28 PM | 显示全部楼层
回复 37# JamesTea


   
那么如果:

If statement A
   If statement B
      x=y+z;
       If statement C
        x=y++;
else
    x=y-z;
    x=z++;


如果 statement A true, 就 statement B? 如果 B true, 就 statement C? 如果 statement C false, 就 else? 是 x=y-z 吗?
有 3 个 if , 但只有 2 个 else, 那么那两个 else 是 for inner if 的吗?
如 statement A false, no else, just print output? 如 statement B false, then go else x=z++? 如 statement C false, then go x=y-z?
回复

使用道具 举报

发表于 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 里面跑罢了。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 20-4-2024 01:58 PM , Processed in 0.061532 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表