这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
zh:courses:java2025:ch02 [2025/02/16 10:55] pzczxs [程序控制流:if] |
zh:courses:java2025:ch02 [2025/04/04 11:26] (当前版本) pzczxs 讨论状态变化了 |
||
---|---|---|---|
行 298: | 行 298: | ||
===== 上机作业 ===== | ===== 上机作业 ===== | ||
- | * 习题3.11:编写程序,求$1 + 3 + 7 + 15 + 31 + \cdots + (2^{20} - 1)$的值; | + | * 编写程序,求$1 + 3 + 7 + 15 + 31 + \cdots + (2^{20} - 1)$的值; |
- | * 习题3.12:已知$S = 1 - \frac{1}{2} + \frac{1}{3} - \frac{1}{4} + \cdots + \frac{1}{n - 1} - \frac{1}{n}$,试编写程序求解直到满足$\frac{1}{n} < {10}^{-5}$时的$S$值; | + | 【参考答案】 |
+ | <file java TowPowSummation.java> | ||
+ | package cn.edu.bjut.chapter2; | ||
+ | |||
+ | public class TwoPowerSummation { | ||
+ | public static void main(String[] args) { | ||
+ | int sum = 0; | ||
+ | for (int n = 1; n <= 20; n++) { | ||
+ | sum += (int) Math.pow(2, n) - 1; | ||
+ | } | ||
+ | |||
+ | System.out.println(sum); | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
+ | * 已知$S = 1 - \frac{1}{2} + \frac{1}{3} - \frac{1}{4} + \cdots + \frac{1}{n - 1} - \frac{1}{n}$,试编写程序求解直到满足$\frac{1}{n} < {10}^{-5}$时的$S$值; | ||
+ | 【参考答案】 | ||
+ | <file java FractionSummation.java> | ||
+ | package cn.edu.bjut.chapter2; | ||
+ | |||
+ | public class FractionSummation { | ||
+ | public static void main(String[] args) { | ||
+ | double sum = 0; | ||
+ | for (int n = 1; (1.0 / n >= 1e-5); n++) { | ||
+ | if (n % 2 == 0) { // 偶数 | ||
+ | sum -= 1.0 / n; | ||
+ | } else { // 奇数 | ||
+ | sum += 1.0 / n; | ||
+ | } | ||
+ | } | ||
+ | System.out.println(sum); | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
* 编写程序,计算没有数码9的三位数有多少个,它们的和等于多少? | * 编写程序,计算没有数码9的三位数有多少个,它们的和等于多少? | ||
+ | 【参考答案】 | ||
+ | <file java ThreeDigitNumber.java> | ||
+ | package cn.edu.bjut.chapter2; | ||
+ | |||
+ | public class ThreeDigitNumber { | ||
+ | public static void main(String[] args) { | ||
+ | int count = 0; | ||
+ | int sum = 0; | ||
+ | |||
+ | for (int i = 100; i < 1000; i++) { | ||
+ | String str = String.valueOf(i); | ||
+ | if (str.indexOf('9') >= 0) { | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | count++; | ||
+ | sum += i; | ||
+ | } | ||
+ | |||
+ | System.out.println(count + "\t" + sum); | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
* 国际专利分类号(IPC)的前15位由section(部),class(大类),subclass(小类),main group(大组)和sub group(小组)五部分组成,每位的要求如下表所示,具体例子如:“G06F 17/30”和“H01M 10/587”,编写程序将这五部分拼接成IPC的实际表现形式; | * 国际专利分类号(IPC)的前15位由section(部),class(大类),subclass(小类),main group(大组)和sub group(小组)五部分组成,每位的要求如下表所示,具体例子如:“G06F 17/30”和“H01M 10/587”,编写程序将这五部分拼接成IPC的实际表现形式; | ||
行 311: | 行 367: | ||
| 10-14 | subgroup(左对齐) | 0...99999 | | | 10-14 | subgroup(左对齐) | 0...99999 | | ||
+ | 【参考答案】 | ||
+ | <file java Ipc.java> | ||
+ | package cn.edu.bjut.chapter2; | ||
+ | |||
+ | public class Ipc { | ||
+ | public static void main(String[] args) { | ||
+ | final char SEPARATOR = '/'; | ||
+ | |||
+ | { | ||
+ | char section = 'A'; | ||
+ | int mainClass = 4; | ||
+ | char subclass = 'B'; | ||
+ | int mainGroup = 55; | ||
+ | int subgroup = 56; | ||
+ | |||
+ | String classification = section | ||
+ | + String.format("%2s", mainClass).replace(' ', '0') | ||
+ | + subclass + String.format("%4s", mainGroup) | ||
+ | + SEPARATOR + subgroup; | ||
+ | |||
+ | System.out.println(classification); | ||
+ | } | ||
+ | |||
+ | { | ||
+ | String classification = "A04B 55/56"; | ||
+ | char section = classification.charAt(0); | ||
+ | int mainClass = Integer.parseInt(classification.substring(1, 3)); | ||
+ | char subclass = classification.charAt(3); | ||
+ | int pos = classification.indexOf(SEPARATOR); | ||
+ | int mainGroup = Integer.parseInt(classification.substring(4, pos).trim()); | ||
+ | int subgroup = Integer.parseInt(classification.substring(pos + 1)); | ||
+ | |||
+ | System.out.println(section + "\t" + mainClass + "\t" | ||
+ | + subclass + "\t" + mainGroup + "\t" | ||
+ | + subgroup); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
[[zh:courses:java2025:index|返回Java课程页]] | [[zh:courses:java2025:index|返回Java课程页]] | ||
- | ~~DISCUSSION~~ | + | ~~DISCUSSION:closed~~ |