I think it depends on whether the list is odd or even in length. Actually, that's probably not true. With one, it'll probably always get it down there, but if I've made it just equal to two I might have lost.
是奇数还是偶数,事实上,这是不正确的,如果最后剩下一个,那可能得到了结果,如果剩下两个,可能错了,所以,首先我们要格外。
And then in the even case I've got to do a square and the divide.
如果是偶数情况的话,我还要再做一次除法和求平方。
It's not as though, oh, today is odd but maybe tomorrow it will be even.
并不是说,哦,今天它是奇数,也许明天就是偶数了。
In either case, I take that value and multiply back by two, if it was even I get back the original number, if it was odd, I'm not going to get back the original number, so I can just check to see if they're the same.
在两种情况中,我都把结果值,再乘以2,如果x是偶数我就,得到了原来的值,如果x是奇数那么就不会得到,原来的值,因此我看看得到的值,和原来的值相等不相等就可以了。
If x was even, x divided by two is going to be actually the half of x, right?
如果x是偶数的话,x除以2当然,就等于x的一半了,对不对?
It says, in either case in general, t of b-- and this is where I'm going to abuse notation a little bit but I can basically bound it by t, 12 steps plus t of b over 2.
我可以用一个,比12+t的数代表,这里有点不准确的地方,具体的步数依赖于奇数偶数,但是你们可以看到在两个case中。
I didn't just make it one smaller, I made it a half smaller. That's a nice deal.OK.
我把它的规模减半了,这太棒了,好,但是我不会直接处理b是偶数这种情况的。
Don't sweat the details here, I just want you to see the reason it goes through it.
现在我们注意到,这样做确实不错,这意味着,无论b是偶数还是奇数。
t In the b even case, again I'm going to let t of b be the number of steps I want to go through.
如果b为偶数,那么我还是要用,来代表解决这个问题需要的步骤数。
It's going to print out even or odd, depending on whether the value I put in, which is x in this case, is even or odd.
它会打印奇数或者偶数,这取决于我输入的值,也就是这个例子中的x,是奇数还是偶数。
Remember last time, we wrote this simple piece of code to print out even or odd. If, you know, x, it was in fact, even or odd. So let me show you what a flow chart for that would look like, because I want to make a comparison point here.
记得上节课我们写过的,那个显示奇数还是偶数的程序么,如果,你知道,x,实际上是奇数还是偶数,那么让我来为大家画个这个程序的流程图吧,因为我这里想为大家做个比较。
On the next step though, this, we get substituted by that. Right, on the next step, I'm back in the even case, it's going to take six more steps, plus t of b minus 1. Oops, sorry about that, over 2.
这一步就是偶数了,这一步会让我们得到,6+t这样的结果,因为b-1现在是偶数了,别忽略这里的细节。
Here's another way I could do that.
如果b是偶数的话,a的b次方。
You know, that's nice, right?
因为这样即使b是奇数,b-1也是偶数?
应用推荐