用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 17:03:43 浏览: 372
```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;
}
```
相关问题
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
```
判断hello wor ld.sh是否具有写权限?
可以使用`-w`选项来判断文件是否具有写权限。具体命令如下:
```bash
if [ -w "hello wor ld.sh" ]; then
echo "hello wor ld.sh具有写权限"
else
echo "hello wor ld.sh没有写权限"
fi
```
在上面的代码中,`[ -w "hello wor ld.sh" ]`表示判断文件`hello wor ld.sh`是否具有写权限。如果具有写权限,则输出“hello wor ld.sh具有写权限”,否则输出“hello wor ld.sh没有写权限”。
阅读全文