打印此序列的算术表达式是什么? [英] What is arithmetic Expression that print this Sequence?
本文介绍了打印此序列的算术表达式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
打印此序列的算术表达式是什么?
8,7,8,7,8,7,.......
What is arithmetic Expression that print this Sequence?
8,7,8,7,8,7,.......
x=??;
While(true){
x=[??????]; //Arithmetic Expression.
System.Out.Print(x+",");
}
推荐答案
尝试类似
? = x 0
8 = x 1 = c-x 0 →x 0 = c-8
7 = x 2 = c-x 1 →x 1 = c-7 = 8→c =? →x 0 =?
...
... = x n + 1 = c-x n
那么,c是什么,x 0 是什么?
说的太多了吗?
玩得开心!
干杯
Andi
Try something like
? = x0
8 = x1 = c - x0 → x0 = c - 8
7 = x2 = c - x1 → x1 = c - 7 = 8 → c = ? → x0 = ?
...
... = xn+1 = c - xn
So, what is c and what is x0?
Too much said?
Have fun!
Cheers
Andi
如果您要求的是位运算表达式(&, |, ~
)而不是算术表达式(+, -, *, /, %
),则这些特定值有一个简单的解决方案:
x = ~x & 0x0F;
原因:7的位模式是00000111
,8的位模式是00001000
-> 7和8是最低4位中彼此的反位.
~00000111 & 00001111 = 00001000
~00001000 & 00001111 = 00000111
干杯
Andi
If you asked for a bit operations expression (&, |, ~
) instead of an arithmetic expression (+, -, *, /, %
), the particular values have a simple solution:
x = ~x & 0x0F;
Reason: the bit pattern of 7 is00000111
and of 8 is00001000
--> 7 and 8 are the inverse bits of each other in the lowest 4 bits.
~00000111 & 00001111 = 00001000
~00001000 & 00001111 = 00000111
Cheers
Andi
这个问题的解决方案需要一分钟左右的时间:x = 7; /* … */ x = (x % 2) + 7;
就可以了.
另一种解决方案:x = x + 2 * (x & 1) - 1;
在除法运算符比加法运算符慢得多的CPU上,它可能是最快的.和位掩码运算符总是非常快.
更准确地说,由于安德烈亚斯·吉列(Andreas Gieriet)的出色发言,更快的解决方案是:x = x + ((x & 1) << 1) - 1;
—SA
The solution of this problem requires a minute or so:x = 7; /* … */ x = (x % 2) + 7;
that''s it.
One more solution:x = x + 2 * (x & 1) - 1;
it can be the fastest on the CPUs where division operators are much slower than additive ones; and bit mask operators are always fast.
More exactly, even faster solution, thanks to the great note by Andreas Gieriet, would be this:x = x + ((x & 1) << 1) - 1;
—SA
这篇关于打印此序列的算术表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文