| English | 释义 | 拼音 |
|---|---|---|
| accumulator/əˈkjuːmjʊleɪtə/ | 累加器 | lěi jiā qì |
A zero initial maximum invents a result
- An array contains −8, −3 and −11. Starting max at zero leaves a maximum that was never in the array.
- For this nonempty array, start max at a[0]. Comparing the later values updates −8 to −3 and keeps it there.
![A zero initial maximum invents a result: for the array −8, −3, −11, starting max at 0 returns 0, a value never in the array; starting at a[0] gives −8, then −3, which stays; an empty array needs a separate check](/learn-media/ap_computer_science_a/assets/0209-max-start.png?v=1794647927)
Loop patterns: accumulate
- A loop pattern is a reusable recipe for a common task. The accumulator 累加器 pattern builds a running total or product across iterations.
- Start
sum = 0, thensum = sum + xinside the loop; the running total changes; it can decrease when a negative value is added. Same idea for a running count (count++) or a running max.
Conditionals inside loops
- Put an
ifinside a loop to act only on items that match. "Count the even numbers": loop, andif (x % 2 == 0) count++; - The loop visits every item; the
ifdecides which ones to process. Combining selection and repetition is the heart of most algorithms.
Finding a maximum
- To find the largest value, keep a
maxvariable and update it when you see something bigger. Check the array is nonempty, startmaxat its first value, then examine the rest. An unspecified “very small number” can still exceed every actual input. if (x > max) { max = x; }inside the loop. After the loop,maxholds the largest value seen.
累加器模式
和 在各遍中累加;它从 0 开始。
要在循环里构建累计的和,你应把累加器初始化为……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
和从 0 开始;积则从 1 开始。
要构建累计的积,把累加器初始化为……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
让积从 0 开始会让它永远是 0;从 1 开始。
在循环时数偶数,你使用……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
if 挑选循环数哪些项。
把累加器的初始化放进循环里会每遍重置它。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
在循环前初始化,而非循环里。
要在找到目标后立即停止循环,你可以用……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
break 退出最内层循环。在循环条件或控制逻辑中还需使用布尔标志;仅赋值不会停止执行。
Early exit with a flag or break
- Sometimes you can stop early once you've found what you need. A
booleanflag can record “found it” and be checked by the loop condition; assigning the flag alone does not exit a loop.breakexits the innermost loop immediately. - "Does the list contain a zero?" — stop at the first zero. Early exit saves work but keep the logic correct for the "not found" case.
Initialize the accumulator correctly, or every result is off. A sum starts at 0, a product at 1 (starting a product at 0 makes it always 0), a max at the first element after a nonempty check. An empty collection has no maximum unless the contract explicitly defines a separate outcome. And update it every matching iteration — a misplaced initialization inside the loop resets it each pass and ruins the total.
What is the maximum of the nonempty array {−8, −3, −11}?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
−3 is the greatest actual element. Starting at 0 produces an invalid invented candidate.
读取 a[0] 是初始化空数组最大值的合法方式。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
空数组没有第一个元素。请检查输入或定义单独的无最大值结果。
整型数组 {-8, -3, -11} 中包含多少个偶数?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
只有 -8 除以 2 余数为零;负值也可以是偶数。
Counting evens in a loop over an array a:
int count = 0;for (int i = 0; i < a.length; i++) { if (a[i] % 2 == 0) count++; }- The loop visits each element; the
ifcounts only the even ones.
Carry the reasoning to a new case
- An empty array has no first element and needs a separate contract or check.
- The initial value must suit the operation: sum zero, product one, maximum a valid starting candidate.
Common loop patterns combine iteration and selection: an accumulator (sum += x, count, or running max) built across passes, with an if inside to process only matching items. Initialize the accumulator right (sum 0, product 1, max the first value), and consider an early exit (break/flag) once the answer is found.