用户工具

站点工具


zh:courses:java2026:ch03

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
zh:courses:java2026:ch03 [2026/03/23 18:57]
pzczxs [上机作业]
zh:courses:java2026:ch03 [2026/03/30 19:18] (当前版本)
pzczxs [上机作业]
行 640: 行 640:
  String symbol = "G09F 17/​30"; ​  String symbol = "G09F 17/​30"; ​
  System.out.println(new Ipc(symbol)); ​  System.out.println(new Ipc(symbol)); ​
 + }
 +}
 +</​file>​
 +  * 定义一个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()等。
 +注意: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)); ​
  }  }
 } }
zh/courses/java2026/ch03.1774263471.txt.gz · 最后更改: 2026/03/23 18:57 由 pzczxs