And I want to remind you, that block of code is a set of instructions, the same set of instructions that are going to be done each time through the loop.
我想提醒大家,这块代码是一个指令集,一个每次循环都会被执行的相同指令集,会变化的仅仅是变量的值。
I'm going to run through a loop, and I'm going to request some input, which I'm going to read in with raw input.
我要运行一个循环,我需要一些输入,需要读入一些原始的输入。
Oh, there's a man at the store and now we have a loop where he is just looking through my inventory one at a time and reciting what it is I actually picked up.
商店里有一个人,现在我们有一个循环,通过这个循环他可以看到我的库存,然后显示出来我实际上都采摘了哪些水果。
We'll loop back as we go through the class to questions about what's gone so horribly wrong, and more important, from a public health point of view, what can be done about it?
当所有的课都上完后我们会再回头看,到底是什么出了问题,更重要的是,从公共健康的角度来看,如何才能纠正这些错误呢
If you look at solve 2, it's going to solve2 run through the same kind of loop, printing out all of the answers.
我会让你们看一看,如果你在看,那将运行相同的循环。
The loop invariant what does the loop invariant mean? It says, here is a property that is true of this structure every time through the loop.
循环不变量是什么意思?,它是一个在结构体中,每次循环都为真的属性。
I'm going to build a list. Right, you can see that little loop going through there.
我要创建一个列表,是的,你可以看到这里有一个小小的循环。
Which means, next time through the loop, that's going to pop out and return an answer.
循环会跳出并返回这个答案,如果不相等的话。
At the end of the first round, I've got the smallest element at the front. At the end of the second round, I've got the smallest two elements at the front, in fact I got all of them sorted out. And it actually runs through the loop multiple times, making sure that it's in the right form.
看看发生了什么,在第一轮结束后,我把最小的元素移到了前面,第二轮结束后,我把最小的,两个元素移到了前面,实际上,所有的元素都排好序了,实际上,这个算法运行了几次循环,确认下这是正确的形式。
So I take 2 plus 3 b steps to go through this loop OK. So if b is 300, it takes 902 steps.
好,如果b是300的话就是902步,如果b是3000那就有9002步。
They're running through a loop. It's a common way of thinking about problems.
有继承意义的迭代程序了,它们是以循环的模式来运行的。
So each time through the loop, I'm doing three steps. Three basic operations.
因此每次运行这个循环,我做了三次基本运算。
You could run through a little loop to say keep trying until you get one. But one of the ways I could deal with it is what's shown here.
你可以运行一个小循环来不断地做,直到得到一个输入,但你应该做的是弄明白这里到底要干什么。
It's basically cycling through the loop a multiplying by a each time.
这个代码就是简单的循环,每次乘一个。
Initialization. A very common bug is to initialize a variable. Go through a loop, and then forget to reinitialize it when it needs to be reinitialized again.
忘记初始化,忘记初始化变量是一个很常见的错误,运行完一个循环之后,当需要再次初始化时。
Go through the second round of the loop.
再做第二遍循环。
And now I want to run through the loop.
现在我想进入循环了。
How many times do I go through the loop?
那我执行了多少次循环呢?
Sorry? OK. You're doing the whole thing for me What's the complexity just of this inner loop m How many times do I go through that loop?
就这一块代码的,我执行了多少次这个循环?,次对不对?,here?,Just,this,piece。,我一会儿就会去得到你的答案的?
But that little short hand there is doing exactly the same thing. It is adding that value into some digits and putting it back or signing it back into some digits. And I'll walk through that loop and when I'm done I can print out the total thing does. And if I do that, I get out what I would expect.
加上得到的这个数的,但是这个缩写声明其实是进行了同样的操作,它把我们得到的这个数加到一个数上面去,然后用和对这个数进行了重新赋值,在循环中会去遍历字符串,当完成循环后,程序会显示数字的总和,如果我运行,这个程序的话,我会得到我期待的结果。
Just to remind you what it does, we bound x to some value, we set up an initial variable called ANS or answer, answer and then we run through a little loop.
记住你要做些什么,我们给x赋一个值,我们建立一个初始变量,命名为ANS或者。
It's going to run through the loop, accumulating the answers, but because I'm subtracting, x it's just going to keep making x more and more negative as it goes along, again it's off into an infinite loop.
它一直在循环,不断的累加答案,这是因为我一直在对x做减法,这使得作为负数的,变的越来越小,又进入一个无限循环了。
So first time through the loop, the answer is a. Second time it-sorry, as it enters the loop, at the time it enter exits a the answer is a.
当第一次完成循环的时候,答案是a,第二次,对不起,在第一次进入循环的时候,答案就是。
I'm going to give a name to that. And what you see there is I'm going to call divisors initially an empty tuple, something has nothing in it. Right here. And then I'm going to run through the same loop as before, going through this set of things, doing the check.
你们可以看到这里,我初始化一个空的元组,名为divisiors,这里,然后我会去运行,跟以前一样的循环,遍历这个集合内的东西,然后做检查,现在我要做的是,每次我找到了一个除数我要把它收集起来。
All right, we're-- well actually, I should say that better, we first check to see, is x 0 greater than or equal to zero, if it's not, then we come down here and we print something out, otherwise we run through a little loop to get the answer, and then we check it and we spit something out.
然后我们运行一个小循环,好了,我们现在来看看,如果x≥0或者x<,我们看这儿,我们会输出一些内容,或者我们运行一个小循环来获取答案,然后我们检验一下,然后得到结果。
And then I run a loop in which I read something in, I check to see if it's the right type, if it is, I change that variable to say it's now the correct type, which means the next time through the loop, I'm going to say I'm all set and I'm going to bounce out.
因为我还没输入,然后我运行一个循环,循环内部我输入一些东西,然后看看是不是正确的类型,如果是的话我改变input,Ok为真,来证明输入数是正确的类型,这就意味着下一次运行这个循环的时候,就可以跳出循环继续执行了。
How many times do I go through that loop?
我都要去做m个操作?
Next time through the loop it goes to a cubed.
然后再一次循环变成a的三次方。
b times. So I've got to go 3 b steps. All right I've got to go through the loop b times I've got three steps each time, and then when I pop out of the loop, I've got two more steps. All right I've got the initiation of answer and the return of it.
好,我一共执行了b次循环,每次循环内部进行了三次基本操作,当跳出循环后还有两个基本操作,因此这个初始的答案就是,2+3b次基本操作,来完成这个循环。
应用推荐