这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
|
zh:courses:java2026:ch03 [2026/03/23 19:28] pzczxs [上机作业] |
zh:courses:java2026:ch03 [2026/03/30 19:18] (当前版本) pzczxs [上机作业] |
||
|---|---|---|---|
| 行 644: | 行 644: | ||
| </file> | </file> | ||
| * 定义一个StringUtils类,至少包括方法isIpc()和capitalize()等。 | * 定义一个StringUtils类,至少包括方法isIpc()和capitalize()等。 | ||
| + | 【参考答案】 | ||
| + | <file java StringUtils.java> | ||
| + | package cn.edu.bjut.utils; | ||
| + | |||
| + | public class StringUtils { | ||
| + | public final static char SEPARATOR = '/'; | ||
| + | |||
| + | public static String capitalize(String str) { | ||
| + | if (str == null) { | ||
| + | return null; | ||
| + | } | ||
| + | |||
| + | if (str.length() == 1) { | ||
| + | return str.toUpperCase(); | ||
| + | } | ||
| + | |||
| + | return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase(); | ||
| + | } | ||
| + | |||
| + | public static boolean isIpc(String str) { | ||
| + | char section = str.charAt(0); | ||
| + | if (section < 'A' || section > 'H') { | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | int mainClass = Integer.parseInt(str.substring(1, 3)); | ||
| + | if (mainClass < 1 || mainClass > 99) { | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | char subclass = str.charAt(3); | ||
| + | if (subclass < 'A' || subclass > 'Z') { | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | int pos = str.indexOf(SEPARATOR); | ||
| + | int mainGroup = Integer.parseInt(str.substring(4, pos).trim()); | ||
| + | if (mainGroup < 1 || mainGroup > 9999) { | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | int subgroup = Integer.parseInt(str.substring(pos + 1)); | ||
| + | if (subgroup < 0 || subgroup > 99999) { | ||
| + | return false; | ||
| + | } | ||
| + | |||
| + | return true; | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | String str = "heLLO"; | ||
| + | System.out.println(StringUtils.capitalize(str)); | ||
| + | |||
| + | String symbol = "G06F 17/30"; | ||
| + | System.out.println(StringUtils.isIpc(symbol)); | ||
| + | } | ||
| + | } | ||
| + | </file> | ||
| * 定义一个MathUtils类,至少包括方法sum()和LogSumExp()等。 | * 定义一个MathUtils类,至少包括方法sum()和LogSumExp()等。 | ||
| - | LogSumExp的原始计算公式为$y = \log \sum_i \{e^{x_i}\}$, | + | 注意:LogSumExp的计算公式为$y = \log \sum_i {e^{x_i}}$。 |
| + | |||
| + | 该公式有以下两个问题:(1)当$x_i$有一个偏大时,$e^{x_i}$计算值会溢出(overflow),致使数值计算出现问题;(2)当$x_i$都偏小时,$\sum_i {e^{x_i}}$接近于0,对其计算log也会出现数值计算问题。 | ||
| + | 【参考答案】 | ||
| + | <file java MathUtils.java> | ||
| + | package cn.edu.bjut.utils; | ||
| + | |||
| + | public class MathUtils { | ||
| + | public static double logsum(double loga, double logb) { | ||
| + | double sum = (loga > logb)? | ||
| + | (loga + Math.log(1.0 + Math.exp(logb - loga))): (logb + Math.log(1.0 + Math.exp(loga - logb))); | ||
| + | |||
| + | return sum; | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | double loga = 10; | ||
| + | double logb = 1000; | ||
| + | |||
| + | double sum = MathUtils.logsum(loga, logb); | ||
| + | System.out.println(sum); | ||
| + | System.out.println(Math.exp(1000)); | ||
| + | } | ||
| + | } | ||
| + | </file> | ||
| [[zh:courses:java2026:index|返回Java课程页]] | [[zh:courses:java2026:index|返回Java课程页]] | ||
| ~~DISCUSSION~~ | ~~DISCUSSION~~ | ||