用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 12:03:43 浏览: 394
```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
```
Cells(2, 11).Value ="Check ok" Cells(2, 12).Value ="Check " Cells(2, 13).Value ="Check" If ThisWorkbook.Worksheets("check").Cells(2, 11).Value <> "Check ok" And ThisWorkbook.Worksheets("check").Cells(2, 12).Value <> "Check ok" And ThisWorkbook.Wor 這段代碼為什麼判斷出來的結果不對
根据您提供的代码,条件判断部分存在问题。根据您提供的情况,`Cells(2, 11).Value`的值为"Check ok",这意味着第一个条件将为假,不满足判断条件。因此,整个条件判断表达式的结果将为假。
您所期望的判断结果应该是根据单元格的值是否等于"Check ok"来确定。如果您希望只有在这三个单元格的值都不等于"Check ok"时才满足条件,可以使用逻辑运算符`And`来组合这三个条件:
```vb
If Cells(2, 11).Value <> "Check ok" And Cells(2, 12).Value <> "Check ok" And Cells(2, 13).Value <> "Check ok" Then
' 执行条件满足时的代码
End If
```
如果以上代码的判断结果仍然不符合您的预期,请提供更多相关代码或上下文信息,以便我更好地帮助您解决问题。
阅读全文