IB Computer Science: Calculation Practice | IB计算机科学计算题专项训练

📚 IB Computer Science: Calculation Practice | IB计算机科学计算题专项训练

Mastering calculation skills is a vital part of success in IB Computer Science. From number systems and logic to performance metrics and networking, students must be confident in applying formulae and performing precise computations. This article presents a focused set of worked examples with step‑by‑step explanations, designed to build fluency and exam confidence.

掌握计算技能是 IB 计算机科学取得成功的关键一环。从数制系统、逻辑运算到性能指标与网络,学生必须能自信地应用公式并进行精确计算。本文提供了一组精选的典型例题,配以分步解析,旨在帮助大家提升熟练度与考试信心。


1. Binary–Decimal Conversion | 二进制与十进制转换

To convert a binary number to decimal, sum the products of each bit and its positional power of 2. For example, binary 101101₂ equals 1×2⁵ + 0×2⁴ + 1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 32 + 0 + 8 + 4 + 0 + 1 = 45₁₀.

要将二进制数转换为十进制,把每一位与其对应的 2 的位权相乘再求和。例如二进制 101101₂ 等于 1×2⁵ + 0×2⁴ + 1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 32 + 0 + 8 + 4 + 0 + 1 = 45₁₀

To convert decimal to binary, repeatedly divide by 2 and record the remainders. Convert 156₁₀ to binary: 156÷2=78 R0, 78÷2=39 R0, 39÷2=19 R1, 19÷2=9 R1, 9÷2=4 R1, 4÷2=2 R0, 2÷2=1 R0, 1÷2=0 R1. Reading remainders upwards gives 10011100₂.

十进制转二进制则通过反复除以 2 并记录余数来实现。将 156₁₀ 转换为二进制:156÷2=78 余0, 78÷2=39 余0, 39÷2=19 余1, 19÷2=9 余1, 9÷2=4 余1, 4÷2=2 余0, 2÷2=1 余0, 1÷2=0 余1。从下往上读取余数得 10011100₂


2. Hexadecimal and Octal | 十六进制与八进制

Hexadecimal uses base 16 with digits 0–9 and A–F (A=10, B=11, …, F=15). To convert binary to hex, group bits in fours from the right. Binary 110101110₂ becomes 1 1010 1110 → 1 A E = 1AE₁₆. To convert hex to decimal, expand by powers of 16: 1AE₁₆ = 1×16² + 10×16¹ + 14×16⁰ = 256 + 160 + 14 = 430₁₀.

十六进制使用基数 16,数字为 0–9 和 A–F(A=10, B=11, …, F=15)。二进制转十六进制时,从右向左每四位一组。二进制 110101110₂ 分组为 1 1010 1110 → 1 A E = 1AE₁₆。十六进制转十进制则按 16 的幂展开:1AE₁₆ = 1×16² + 10×16¹ + 14×16⁰ = 256 + 160 + 14 = 430₁₀

For octal (base 8), group binary digits in threes. The same binary number 110101110₂ → 110 101 110 → 6 5 6 = 656₈. Practice: Convert 756₈ to decimal. 7×8² + 5×8¹ + 6×8⁰ = 7×64 + 40 + 6 = 448+40+6 = 494₁₀.

八进制(基数 8)则将二进制每三位一组。同一二进制数 110101110₂ 分组为 110 101 110 → 6 5 6 = 656₈。请练习:将 756₈ 转为十进制。7×8² + 5×8¹ + 6×8⁰ = 7×64 + 40 + 6 = 448+40+6 = 494₁₀


3. Two’s Complement Representation | 补码表示

In two’s complement, the most significant bit (MSB) carries a negative weight. For an 8‑bit number, the MSB represents −128. Convert the signed binary 11001010₂ to decimal: −128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = −54₁₀.

在补码表示中,最高有效位(MSB)带有负权重。对于 8 位数,MSB 代表 −128。将带符号二进制数 11001010₂ 转为十进制:−128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = −54₁₀

To find the two’s complement of a negative decimal, first write the positive magnitude in binary, then invert the bits and add 1. Find −27 in 8‑bit two’s complement: +27 = 00011011₂, invert → 11100100, add 1 → 11100101₂. Check: −128 + 64 + 32 + 0 + 0 + 4 + 0 + 1 = −128 + 101 = −27.

求一个负十进制数的补码时,先写出其正数的二进制,然后所有位取反再加 1。求 −27 的 8 位补码:+27 = 00011011₂,取反 → 11100100,加 1 → 11100101₂。验证:−128 + 64 + 32 + 0 + 0 + 4 + 0 + 1 = −128 + 101 = −27。


4. Floating Point Representation | 浮点数表示

Floating point uses a mantissa and an exponent to store real numbers. In a simplified 8‑bit representation with 5‑bit mantissa (M) and 3‑bit exponent (E), the value is M × 2ᴱ. Both are in two’s complement. Example: mantissa 01101 (+13), exponent 010 (+2) → value = 13 × 2² = 52₁₀. If mantissa = 10110 (−10) and exponent = 111 (−1), value = −10 × 2⁻¹ = −5₁₀.

浮点数使用尾数和指数来存储实数。在一个简化的 8 位表示法中,假设 5 位尾数(M)和 3 位指数(E),数值为 M × 2ᴱ,两者均采用补码。例如:尾数 01101(+13),指数 010(+2)→ 数值 = 13 × 2² = 52₁₀。若尾数 = 10110(−10),指数 = 111(−1),则数值 = −10 × 2⁻¹ = −5₁₀

Practice: Given 8 bits with 5 M and 3 E, interpret 10011001. Split: M=10011, E=001. M=10011₂ = −16+2+1 = −13, E=001₂ = +1. Value = −13 × 2¹ = −26₁₀.

练习:给定 5 位尾数 + 3 位指数的 8 位模式,解释 10011001。拆分:M=10011, E=001。M=10011₂ = −16+2+1 = −13, E=001₂ = +1。数值 = −13 × 2¹ = −26₁₀


5. Logic Gates and Boolean Expressions | 逻辑门与布尔表达式

Evaluate the Boolean expression for given inputs and draw the truth table. Consider the circuit: Q = (A AND B) OR (NOT C). For A=1, B=0, C=1, we compute: A AND B = 0, NOT C = 0, so Q = 0. The truth table covers 2³ = 8 rows. When A=1, B=1, C=0: A AND B = 1, NOT C = 1, Q = 1.

根据给定输入计算布尔表达式并绘制真值表。考虑电路表达式:Q = (A AND B) OR (NOT C)。当 A=1, B=0, C=1 时,计算:A AND B = 0, NOT C = 0,因此 Q = 0。真值表共有 2³ = 8 行。当 A=1, B=1, C=0 时:A AND B = 1, NOT C = 1, Q = 1。

Simplify a Boolean expression using laws: X = A·B + A·B’ . By factoring, X = A·(B + B’) = A·1 = A. This means the output X is simply equal to A, independent of B. Another example: Y = (A+B)·(A+C) = A + B·C (distributive law).

使用定律化简布尔表达式:X = A·B + A·B’。提取公因子,X = A·(B + B’) = A·1 = A。这表示输出 X 简单地等于 A,与 B 无关。另一个例子:Y = (A+B)·(A+C) = A + B·C(分配律)。


6. Karnaugh Maps and Simplification | 卡诺图与化简

A Karnaugh map (K‑map) provides a visual method to minimise Boolean functions. For a 2‑variable function F(A,B) = Σ(0,1,3), the K‑map is a 2×2 grid. Place 1 in cells for minterms 0 (00), 1 (01), and 3 (11). Group the two adjacent 1s in column 0 (A’B’ and A’B) to obtain the term A’. The isolated 1 at AB is grouped by itself, giving AB. The minimal expression is F = A’ + AB, which simplifies further to F = A’ + B (by absorption).

卡诺图(K‑map)提供了最小化布尔函数的图形化方法。对于二变量函数 F(A,B) = Σ(0,1,3),K‑map 是一个 2×2 表格。在小项 0 (00), 1 (01), 3 (11) 对应的格子中填 1。将第 0 列中两个相邻的 1(A’B’ 和 A’B)圈为一组,得到项 A’。孤立的 1 在 AB 处单独成组,得 AB。最简表达式为 F = A’ + AB,它可进一步化简为 F = A’ + B(吸收律)。

Practice: Simplify G(A,B,C) = Σ(0,2,4,5,6) using a 3‑variable K‑map. Arrange cells for ABC. Groups: minterms 0,2,4,6 form the pattern where C=0 and A and B change, giving C’. Minterms 4 and 5 form AB’ (since A=1, B=0, C changes). Minimal expression: G = C’ + AB’.

练习:使用三变量 K‑map 化简 G(A,B,C) = Σ(0,2,4,5,6)。按 ABC 排列单元格。分组:小项 0,2,4,6 构成 C=0 且 A 和 B 变化的模式,得到 C’。小项 4 和 5 形成 AB’(因为 A=1, B=0, C 改变)。最简表达式:G = C’ + AB’。


7. Memory Address Calculation | 内存地址计算

Main memory is organised into addressable locations. Given a memory with 16‑bit address bus, the number of addressable locations = 2¹⁶ = 65,536 = 64K. If each location stores 1 byte, the total capacity is 64 KB. For a 32‑bit address bus, capacity = 2³² bytes = 4 GB.

主存储器被组织成可寻址的单元。若地址总线为 16 位,则可寻址单元数 = 2¹⁶ = 65,536 = 64K。若每个单元存储 1 字节,则总容量为 64 KB。对于 32 位地址总线,容量 = 2³² 字节 = 4 GB。

Calculate the number of chips needed to construct a 256 KB memory using 32K × 8 RAM chips. Each chip provides 32 KB (since 32K × 8 = 32 KB). Number of chips = 256 KB / 32 KB = 8 chips. If chips are 16K × 4, each gives 16K × 0.5 B = 8 KB; then 256/8 = 32 chips required.

计算使用 32K × 8 的 RAM 芯片构建 256 KB 存储器所需的芯片数量。每片容量为 32 KB(因 32K × 8 = 32 KB)。芯片数 = 256 KB / 32 KB = 8 片。若芯片为 16K × 4,每片 16K × 0.5 B = 8 KB,则需 256/8 = 32 片。


8. CPU Performance Metrics | CPU性能指标

CPU execution time = (Instruction Count × CPI) / Clock Rate. CPI is the average clock cycles per instruction. If a program has 2 × 10⁹ instructions, CPI = 1.5, and clock rate is 2 GHz (2 × 10⁹ cycles/s), time = (2×10⁹ × 1.5) / (2×10⁹) = 1.5 seconds.

CPU 执行时间 = (指令数 × CPI)/ 时钟频率。CPI 是每条指令的平均时钟周期数。若某程序有 2 × 10⁹ 条指令,CPI = 1.5,时钟频率为 2 GHz(2 × 10⁹ 周期/秒),则时间 = (2×10⁹ × 1.5) / (2×10⁹) = 1.5 秒。

Speedup from pipeline: Ideal speedup = number of stages. With a 5‑stage pipeline, the maximum speedup is 5, but due to hazards the actual speedup is less. If the ideal CPI is 1 and pipelining reduces CPI to 0.25, speedup = 1 / 0.25 = 4.

流水线的加速比:理想加速比 = 流水线段数。对于 5 级流水线,最大加速比为 5,但由于冒险,实际加速比更低。若理想 CPI 为 1,流水线使 CPI 降至 0.25,则加速比 = 1 / 0.25 = 4。


9. Cache Hit Rate and Average Access Time | 缓存命中率与平均访问时间

Average memory access time (AMAT) = Hit time + (Miss rate × Miss penalty). If cache hit time = 1 ns, miss penalty = 20 ns, and miss rate = 5%, AMAT = 1 + 0.05 × 20 = 1 + 1 = 2 ns. Reducing miss rate significantly improves performance.

平均内存访问时间(AMAT)= 命中时间 + (缺失率 × 缺失代价)。若缓存命中时间 = 1 ns,缺失代价 = 20 ns,缺失率 = 5%,则 AMAT = 1 + 0.05 × 20 = 1 + 1 = 2 ns。降低缺失率可显著提升性能。

Calculate the effective CPI with cache: Base CPI = 1.0, instruction cache miss rate = 2%, data cache miss rate = 4%, frequency of data accesses = 40% of instructions, miss penalty = 100 cycles. Memory stall cycles = (0.02 × 100) + (0.4 × 0.04 × 100) = 2 + 1.6 = 3.6 cycles per instruction. Effective CPI = 1.0 + 3.6 = 4.6.

计算考虑缓存后的有效 CPI:基本 CPI = 1.0,指令缓存缺失率 = 2%,数据缓存缺失率 = 4%,数据访问频率为指令的 40%,缺失代价 = 100 周期。内存停顿周期 = (0.02 × 100) + (0.4 × 0.04 × 100) = 2 + 1.6 = 3.6 周期/指令。有效 CPI = 1.0 + 3.6 = 4.6。


10. Network Subnetting (IPv4) | 网络子网划分

Given an IP address 192.168.1.0/24, create 4 subnets. The /24 mask means the first 24 bits are network. Borrow 2 host bits for subnetting: new mask /26 (255.255.255.192). Subnets: 192.168.1.0/26, 192.168.1.64/26, 192.168.1.128/26, 192.168.1.192/26. Each subnet has 2⁶ − 2 = 62 usable host addresses (subtracting network and broadcast addresses).

给定 IP 地址 192.168.1.0/24,创建 4 个子网。/24 掩码表示前 24 位为网络位。借用 2 位主机位进行子网划分:新掩码 /26(255.255.255.192)。子网为:192.168.1.0/26, 192.168.1.64/26, 192.168.1.128/26, 192.168.1.192/26。每个子网有 2⁶ − 2 = 62 个可用主机地址(扣除网络地址和广播地址)。

Calculate the number of subnets and hosts per subnet for a /20 mask derived from a Class B address. Class B default mask /16; borrowed 4 bits, so subnets = 2⁴ = 16. Host bits = 32 − 20 = 12, hosts per subnet = 2¹² − 2 = 4094.

计算从一个 B 类地址派生的 /20 掩码的子网数和每子网主机数。B 类默认掩码 /16;借用 4 位,子网数 = 2⁴ = 16。主机位数 = 32 − 20 = 12,每子网主机数 = 2¹² − 2 = 4094。


11. Algorithm Complexity (Big O) | 算法复杂度(大O)

Determine the time complexity of a nested loop. For i from 0 to n‑1, for j from 0 to n‑1, do work O(1). Total iterations = n × n = n², so complexity is O(n²). If the inner loop runs up to i, total steps = n(n+1)/2, still O(n²).

确定嵌套循环的时间复杂度。for i 从 0 到 n‑1, for j 从 0 到 n‑1, 执行 O(1) 工作。总迭代次数 = n × n = n²,因此时间复杂度为 O(n²)。若内层循环运行到 i,总步数 = n(n+1)/2,仍为 O(n²)。

Analyse a recursive algorithm: T(n) = 2T(n/2) + n. This is the merge sort recurrence. By Master theorem, a=2, b=2, f(n)=n; n^(log_b a) = n^(log₂ 2) = n¹. Case 2 applies, T(n) = Θ(n log n). Contrast with T(n) = T(n-1) + 1 (linear recursion) which gives Θ(n).

分析递归算法:T(n) = 2T(n/2) + n。这是归并排序的递推式。根据主定理,a=2, b=2, f(n)=n;n^(log_b a) = n^(log₂ 2) = n¹。适用情况 2,T(n) = Θ(n log n)。对比 T(n) = T(n-1) + 1(线性递归)可得 Θ(n)。


12. Sorting and Searching Comparisons | 排序与搜索比较

Calculate the maximum number of comparisons in binary search for a sorted array of 1000 elements. Maximum steps = ⌈log₂ 1000⌉ = 10 (since 2⁹ = 512, 2¹⁰ = 1024). A linear search would need up to 1000 comparisons in the worst case.

计算对一个有 1000 个元素的有序数组进行二分查找的最大比较次数。最大步数 = ⌈log₂ 1000⌉ = 10(因为 2⁹ = 512, 2¹⁰ = 1024)。而线性查找在最坏情况下需要多达 1000 次比较。

For bubble sort on n elements, the total number of comparisons is approximately n² / 2. For n=200, that is about 20,000 comparisons. Merge sort, which runs in O(n log n), would take about 200 × log₂ 200 ≈ 200 × 7.64 ≈ 1528 comparisons, demonstrating a vast improvement.

对于 n 个元素的冒泡排序,总比较次数约为 n² / 2。当 n=200 时约为 20,000 次比较。归并排序运行时间为 O(n log n),大约需要 200 × log₂ 200 ≈ 200 × 7.64 ≈ 1528 次比较,这显示出巨大的改进。

Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version