vector<int> s(10); void ss(int num) { if(num==1) s[num]=1; else { s[num]=s[num-1]*2; } } int main() { ss(5); return 0; } 怎么求不出ss(5)
时间: 2024-04-16 16:24:07 浏览: 13
根据你提供的代码,`ss(5)` 求不出结果的原因是 `vector<int> s` 的初始化问题。在你给定的代码中,你使用了默认初始化 `vector<int> s(10)`,这将创建一个大小为 10 的向量,并将所有元素初始化为 0。
在 `ss(int num)` 函数中,你尝试将 `s[num]` 赋值为 `s[num-1]*2`。但是由于向量 `s` 的初始值都是 0,计算 `s[num-1]*2` 时,结果仍然是 0。
为了正确求解 `ss(5)`,你需要确保 `vector<int> s` 中的元素已经被正确初始化。你可以使用以下代码来初始化 `vector<int> s` 中的元素为 1:
```cpp
vector<int> s(10, 1);
```
这将创建一个大小为 10 的向量,并将所有元素初始化为 1。这样,在执行 `ss(5)` 时,`s[5]` 将被赋值为 `s[4]*2`,从而得到正确的结果。
相关问题
vector<int> s(10); void ss(int num) { if (num == 1) { s[num] = 1; return; // 添加递归终止条件 } else { ss(num - 1); s[num] = s[num - 1] * 2; } } int main() { ss(5); cout << s[5] << endl; // 输出结果:16 return 0; } 为什么代码不会自动求s[4]和s[3]的值
代码中的递归函数 `ss(num)` 在计算 `ss(num)` 之前调用了 `ss(num - 1)`,这确保了先计算出 `ss(num - 1)` 的结果。因此,在计算 `ss(5)` 时,会依次计算 `ss(4)`、`ss(3)`、`ss(2)` 和 `ss(1)`,然后再回溯计算 `ss(5)`。
递归的过程如下:
- 计算 `ss(5)`:
- 调用 `ss(4)`
- 调用 `ss(3)`
- 调用 `ss(2)`
- 调用 `ss(1)`
- 设置 `s[1] = 1`
- 设置 `s[2] = s[1] * 2 = 1 * 2 = 2`
- 设置 `s[3] = s[2] * 2 = 2 * 2 = 4`
- 设置 `s[4] = s[3] * 2 = 4 * 2 = 8`
- 设置 `s[5] = s[4] * 2 = 8 * 2 = 16`
因此,当调用 `ss(5)` 时,会自动计算出 `s[4]` 和 `s[3]` 的值,并在回溯过程中将它们赋值给相应的元素。
所以,代码会自动求解 `s[4]` 和 `s[3]` 的值,并最终输出正确的结果:`s[5] = 16`。
#include <iostream> #include <vector> #include <algorithm> #include <sstream>使用命名空间 std;bool isValidSet(int a, int b, int c) { return (a == b && b == c) ||(a + 1 == b && b + 1 == c);} string checkHu(vector<int>& nums) { int n = nums.size(); if (n % 3 != 2) { return “xianggong”; } sort(nums.begin(), nums.end()); for (int i = 0; i < n - 5; i += 3) { if (!isValidSet(nums[i], nums[i + 1], nums[i + 2])) { return “BUHU”; } } if (nums[n - 2] != nums[n - 1]) { return “BUHU”; } return “HU”; } int main() { vector<int> nums; string line; string result; while (true) { getline(cin, 行);if (line == “0”) { break; } stringstream ss(line);整数;while (ss >> num) { nums.push_back(num); } result = checkHu(nums);cout <<结果<< endl;nums.clear();} 返回 0;}如何加以定义个数字的数量不超过4个,集合中元素的数量不超过14个不用返回不胡
如果我们想在 checkHu 函数中判断数字的数量和集合中元素的数量是否符合要求,但是不需要返回 "BUHU" 或 "HU",只需要在 checkHu 函数中进行判断即可。修改后的代码如下:
```
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <unordered_map>
using namespace std;
bool isValidSet(int a, int b, int c) {
return (a == b && b == c) || (a + 1 == b && b + 1 == c);
}
void checkHu(vector<int>& nums) { // 修改返回值类型为 void
int n = nums.size();
if (n % 3 != 2) {
cout << "XIANGGONG" << endl; // 直接输出结果
return;
}
unordered_map<int, int> count;
for (int num : nums) {
count[num]++;
if (count[num] > 4) {
cout << "XIANGGONG" << endl;
return;
}
}
if (count.size() > 14) {
cout << "XIANGGONG" << endl;
return;
}
sort(nums.begin(), nums.end());
for (int i = 0; i < n - 5; i += 3) {
if (!isValidSet(nums[i], nums[i + 1], nums[i + 2])) {
cout << "XIANGGONG" << endl;
return;
}
}
if (nums[n - 2] != nums[n - 1]) {
cout << "XIANGGONG" << endl;
return;
}
cout << "OK" << endl; // 输出结果为 OK
}
int main() {
vector<int> nums;
string line;
while (true) {
getline(cin, line);
if (line == "0") {
break;
}
stringstream ss(line);
int num;
while (ss >> num) {
nums.push_back(num);
}
checkHu(nums); // 调用 checkHu 函数,不需要接收返回值
nums.clear();
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)