Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

bluej - I've been tasked to put this formula to sum 48. Logically it does sum up but I don't know why it rationally sums up 47

enter image description here

Since the variables are a=11, b=3, c=19; I wrote a formula that would sum to 48 like (c++ + a--) + (c++ - b--) when looked logically, (20 + 10) + (20 - 2) which should sum up to 48, but the output is 47.

question from:https://stackoverflow.com/questions/65895358/ive-been-tasked-to-put-this-formula-to-sum-48-logically-it-does-sum-up-but-i-d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pay attention to the difference x++ and ++x and x=x+1, simply you can regard x++ as the following 2 steps:

  1. return x for computing
  2. x=x+1

and the ++x means:

  1. x=x+1
  2. return x for computing

So, the code you write actually compute (19 + 11) + (20 - 3) = 47, and finally a=10, b=2, c=21.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...