用C++写Mr. Wor's router at home is broken again. After some repairs, he needs to reconnect the Ethernet cables to the router. It is known that Mr. Wor's router has n ports, and there are n Ethernet cables. Then, Mr. Wor will sequentially insert the i -th Ethernet cable to the i -th port, for all i from 1 to n . However, due to Mr. Wor's mysterious actions, the first m cables were not inserted into their designated ports, but were inserted into m ports uniformly at random. For the remaining cables, when cable i is being inserted, if port i is not occupied, then cable i will be inserted into port i . Otherwise, cable i will be inserted into a randomly chosen unoccupied port. Mr. Wor wants to know the probability that the last cable, cable n , is plugged into port n . Input Only one line contains two integers n,m(1≤n≤10,0≤m≤n) , denoting the number of cables and the number of cables which was inserted randomly. Output Print the probability in one line. Your answer is considered correct if the relative or absolute error is less than or equal to 10−6 .
时间: 2024-01-29 13:03:43 浏览: 411
```c++
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 15;
double f[N][N];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
f[1][0] = 1;
f[1][1] = 0;
for (int i = 2; i <= n; i++)
{
for (int j = 0; j <= i-1 && j <= n-m; j++)
{
f[i][j] = (i-j)*f[i-1][j]/i + (j+1)*f[i-1][j+1]/i;
}
}
printf("%.8lf", f[n][n-m]);
return 0;
}
```
相关问题
IDEA启动Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Wor
### 解决 `java.lang.ClassNotFoundException` 错误
当在 IDEA 中启动应用程序时遇到 `java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook` 的错误,这通常意味着缺少必要的 Apache POI 库文件。Apache POI 是用于处理 Microsoft Office 文件格式的 Java API。
#### 依赖库缺失分析
该类属于 Apache POI 库的一部分,在编译阶段可能不会报错,但在运行时由于找不到对应的 `.class` 文件而抛出此异常[^2]。因此,确保所有必需的 JAR 包都已正确添加到项目的构建路径中至关重要。
#### 添加所需依赖项的方法
对于 Maven 或 Gradle 构建工具管理下的项目,可以通过修改配置文件来引入所需的依赖:
##### 使用Maven
编辑 pom.xml 并加入以下依赖声明:
```xml
<dependencies>
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<!-- Apache POI - OOXML 支持 (XSSF) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<!-- Commons Math3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
```
更新完 POM 文件之后,执行命令刷新依赖关系树以下载并安装新的 jar 包及其传递性依赖项。
##### 手动导入JAR包
如果项目不是通过上述自动化方式管理,则需手动获取最新版本的 Apache POI 及其相关组件,并将其放置于工程根目录下或指定位置,再将这些外部库添加至 IDE 的模块设置里作为全局可用资源。
#### 验证环境变量与 Classpath 设置
确认 JVM 能够访问到所有的第三方库,检查是否已经把它们放入了合适的 CLASSPATH 环境变量内;另外还需注意不同操作系统间分隔符的区别(Windows 下用`;`, Linux/macOS 则采用`:`)。
#### 清理缓存重新部署应用
有时旧版残留数据可能会干扰新加载的内容,尝试清理本地仓库中的临时文件夹以及重启开发工具和服务端容器后再试一次。
c++ s.assign
`std::string`类的`assign()`函数用于将一个字符串或字符数组的内容赋值给该字符串对象。具体使用方式如下:
```cpp
string& assign (const string& str);
string& assign (const string& str, size_t subpos, size_t sublen);
string& assign (const char* s);
string& assign (const char* s, size_t n);
string& assign (size_t n, char c);
template <class InputIterator>
string& assign (InputIterator first, InputIterator last);
```
- `assign(const string& str)`:将`str`字符串的内容赋值给该字符串对象。
- `assign(const string& str, size_t subpos, size_t sublen)`:将`str`字符串从下标`subpos`开始的长度为`sublen`的子串内容赋值给该字符串对象。
- `assign(const char* s)`:将C风格的字符串`s`的内容赋值给该字符串对象。
- `assign(const char* s, size_t n)`:将C风格的字符串`s`前`n`个字符的内容赋值给该字符串对象。
- `assign(size_t n, char c)`:将`n`个字符`c`的内容赋值给该字符串对象。
- `template <class InputIterator> assign(InputIterator first, InputIterator last)`:将迭代器区间`[first, last)`范围内的内容赋值给该字符串对象。
下面是一个示例:
```cpp
#include <string>
#include <iostream>
int main() {
std::string s1("hello");
std::string s2("world");
s1.assign(s2); // 将s2的内容赋值给s1
std::cout << s1 << std::endl; // 输出:world
s1.assign(s2, 2, 3); // 将s2从下标2开始,长度为3的子串赋值给s1
std::cout << s1 << std::endl; // 输出:rld
s1.assign("hello"); // 将C风格字符串"hello"的内容赋值给s1
std::cout << s1 << std::endl; // 输出:hello
s1.assign("world", 3); // 将C风格字符串"world"的前3个字符的内容赋值给s1
std::cout << s1 << std::endl; // 输出:wor
s1.assign(5, 'a'); // 将5个字符'a'的内容赋值给s1
std::cout << s1 << std::endl; // 输出:aaaaa
std::string s3("hello, world");
s1.assign(s3.begin() + 7, s3.end()); // 将s3从下标7开始到结尾的子串赋值给s1
std::cout << s1 << std::endl; // 输出:world
return 0;
}
```
输出结果为:
```
world
rld
hello
wor
aaaaa
world
```
阅读全文
相关推荐











