diff --git a/README.md b/README.md index d1b20984ca04abf9469a28b823710c69ba2cc5e3..537e925179772985f1659ee5cd58799ec6f22a52 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ git clone git@codechina.csdn.net:csdn/skill_tree_java.git 然后进入 skill_tree_java 目录,执行下列命令安装依赖: ```shell -pip install -r requrirements.txt +pip install -r requirements.txt ``` 然后执行下列命令安装钩子: diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" index cf6dd917367880228317cdf7ba24915782773ee5..9ac06ffce973c75d95dacb883c4edba6213776df 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" @@ -8,7 +8,8 @@ "export": [ "call.json", "parser.json", - "permutation.json" + "permutation.json", + "override.json" ], "keywords_must": [ "函数", diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/override.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/override.json" new file mode 100644 index 0000000000000000000000000000000000000000..e7ffc67aa620b60722e0699fcb9e65d5e3069673 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/override.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "HansBug", + "source": "override.md", + "notebook_enable": false, + "exercise_id": "0c7313f92ebf4b299f56ff9e9e80b6a7" +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/override.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/override.md" new file mode 100644 index 0000000000000000000000000000000000000000..0a7f83a2f8dd1fe8cfcf0098f981d45f320f05fa --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/override.md" @@ -0,0 +1,127 @@ +# 函数的重写 + +我们需要编写一个函数`gaussian`,使其生成服从正态分布$N\left(\mu, \sigma^2\right)$的随机数。其中$\mu$为数学期望,$\sigma^2$为方差,关于正态分布可以参考[百度百科](https://baike.baidu.com/item/%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83/829892)。现已有函数如下: + +```java +import java.util.Random; + +public class TestMain { + private static final Random random = new Random(); + private static double gaussian(double mu, double sigma2) { + return Math.sqrt(sigma2) * random.nextGaussian() + mu; + } +} +``` + +但上述函数仍然需要我们传入$\mu$和$\sigma^2$的值,这会在实际使用这一函数的时候带来麻烦。 + +为了解决这一问题,我们需要对函数进行重写,使之能快速产生: + +1. 数学期望$\mu = 0$的正态分布随机数 +2. 服从标准正态分布($\mu = 0$,$\sigma^2 = 1$)的随机数 + +以达成简化`gaussian`函数使用的效果,以下选项正确的是? + +## 答案 + +```java +import java.util.Random; + +public class TestMain { + private static final Random random = new Random(); + + private static double gaussian() { + return gaussian(0, 1); + } + + private static double gaussian(double sigma2) { + return gaussian(0, sigma2); + } + + private static double gaussian(double mu, double sigma2) { + return Math.sqrt(sigma2) * random.nextGaussian() + mu; + } +} +``` + +## 选项 + +### Python味的默认值,然而Java并不支持 + +```java +import java.util.Random; + +public class TestMain { + private static final Random random = new Random(); + private static double gaussian(double mu = 0, double sigma2 = 1) { + return Math.sqrt(sigma2) * random.nextGaussian() + mu; + } +} +``` + +### 用法正确,但不符合题目要求 + +```java +import java.util.Random; + +public class TestMain { + private static final Random random = new Random(); + + private static double gaussian() { + return gaussian(0, 1); + } + + private static double gaussian(double mu) { + return gaussian(mu, 1); + } + + private static double gaussian(double mu, double sigma2) { + return Math.sqrt(sigma2) * random.nextGaussian() + mu; + } +} +``` + +### Python味的默认值,Java并不支持,就一个也不成 + +```java +import java.util.Random; + +public class TestMain { + private static final Random random = new Random(); + + private static double gaussian() { + return gaussian(0, 1); + } + + private static double gaussian(double mu, double sigma2 = 1) { + return Math.sqrt(sigma2) * random.nextGaussian() + mu; + } +} +``` + +### 函数重写时的标识符必须可区分 + +```java +import java.util.Random; + +public class TestMain { + private static final Random random = new Random(); + + private static double gaussian() { + return gaussian(0, 1); + } + + private static double gaussian(double sigma2) { + return gaussian(0, sigma2); + } + + private static double gaussian(double mu, double sigma2) { + return Math.sqrt(sigma2) * random.nextGaussian() + mu; + } + + private static double gaussian(double sigma2, double mu) { + return Math.sqrt(sigma2) * random.nextGaussian() + mu; + } +} +``` +