跳到主要内容

考前速记

Quick Reference

TaskCode
Declare 1D array (size 10)Arr = [0] * 10
Declare 2D array (3 × 4)Arr = [[0] * 4 for i in range(3)]
Traverse 1Dfor i in range(len(Arr)):
Traverse 2Dfor r in range(R): for c in range(C):
Read input to arrayArr[i] = int(input(...))
Sum all elementsTotal = Total + Arr[i]
Find maxif Arr[i] > Max: Max = Arr[i]
Searchif Arr[i] == SearchValue:
Global declarationglobal Arr before assignment

Common Patterns

Fill-and-process template
def main():
global Arr
Arr = [0] * 10
for i in range(10):
Arr[i] = int(input())
# process Arr here
Find average
Total = 0
for i in range(len(Arr)):
Total = Total + Arr[i]
Average = Total / len(Arr)

Red Flags — Check These During Exam

Red FlagWhat to Check
global used?If declaring array inside a function, yes
range(1, N+1)Off-by-one — should be range(N)
Arr = [] then Arr[i] = xMust use append() or pre-allocate
[0] * N for 2DShallow copy bug — use comprehension
input() without int()String stored instead of number
Loop bounds wrong way roundRows first, columns second in 2D
if Arr[i] = xShould be == not =
Array name spellingMust match exactly what question gives

Mark Scheme Keywords

备注

M1 = method / structure (loop, function, file open)

A1 = accuracy (correct variable, correct indexing)

B1 = bonus / alternative (type conversion, edge case)

If You Get Stuck

  1. Write the loop first: for i in range(N):
  2. Decide what happens inside: input, calculation, comparison
  3. Check array bounds: 0 to N-1
  4. Check global if inside a function
  5. Test with a small example mentally