使用C++ 问题描述 有 N 个非零且各不相同的整数。请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数)。 输入格式 第一行包含一个正整数 N。(1 ≤ N ≤ 500)。 第二行为 N 个用单个空格隔开的非零整数,每个数的绝对值不超过1000,保证这些整数各不相同。 输出格式 只输出一个整数,即这 N 个数中包含多少对相反数。 样例输入 5 1 2 3 -1 -2 样例输出 2给出代码
时间: 2024-04-12 13:27:27 浏览: 21
```cpp
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
unordered_map<int, int> numCount;
vector<int> nums(N);
for (int i = 0; i < N; i++) {
cin >> nums[i];
numCount[nums[i]]++;
}
int count = 0;
for (int i = 0; i < N; i++) {
int num = nums[i];
if (numCount[-num] > 0) {
count++;
}
}
cout << count << endl;
return 0;
}
```
注意:这段代码是用C++编写的,可以直接运行。它使用了unordered_map来统计每个数出现的次数,并使用vector来保存输入的整数。然后遍历每个数,如果它的相反数也在输入中出现,则相反数对的数量增加。最后输出结果。
相关问题
【问题描述】 有 N 个非零且各不相同的整数。请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数)。 【输入形式】 第一行包含一个正整数 N。(1 ≤ N ≤ 500)。 第二行为 N 个用单个空格隔开的非零整数,每个数的绝对值不超过1000,保证这些整数各不相同。 【输出形式】 只输出一个整数,即这 N 个数中包含多少对相反数。 【样例输入】 5 1 2 3 -1 -2 【样例输出】 2 C++实现
以下是基于题意的 C++ 实现:
```c++
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
int n;
cin >> n;
unordered_set<int> s; // 用哈希表存储
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s.insert(x);
}
int ans = 0;
for (auto x : s) {
if (s.count(-x)) {
ans++;
}
}
cout << ans << endl;
return 0;
}
```
将输入的 n 个数放入一个哈希表中,遍历哈希表中的每一个数,如果它的相反数也在哈希表中,那么就说明找到了一对相反数,将答案加 1。最后输出答案即可。
c++实现:问题2:找到石头对。有N个石头,标号为[0,N-1]。我们知道每个石头的重量,不一定为整数。我们想要找到一对石头,即2个石头,它们的重量差为D。 问题A:正式描述问题,明确定义问题,避免歧义。例如:编写一个函数F,函数的输入是...,函数的输出是...。这个问题可能比你想象的要难,请仔细思考。 问题B: 编写你在问题A中描述的函数。 你的算法的空间复杂度是多少?时间复杂度是多少? 你能够同时实现O(N)的时间复杂度和空间复杂度吗?如果可以,请实现它。 编写一个测试程序来测试你的函数。 a. 请列出你想测试的所有边界情况。 b. 如何验证函数能够正确处理这些边界情况?你能以更系统化的方式做到吗? 问题C(可选):与原问题B相同,但这次我们想找到所有重量差为D的石头对。请注意,对(1,4)和对(4,1)被认为是同一对,因此只需要返回一个。你的算法的时间复杂度应该为O(max(R, N)),其中R是结果对数,N是石头数。
问题A:给定N个石头的重量,要求找到一对石头,它们的重量差为D。定义函数findStonePair,输入参数为石头重量数组stones和石头个数N,输出为一对满足条件的石头的下标,如果不存在这样的一对石头,则返回{-1,-1}。
问题B:以下是C++代码实现,时间复杂度为O(NlogN),空间复杂度为O(1)。
```
#include <vector>
#include <algorithm>
using namespace std;
vector<int> findStonePair(double* stones, int N, double D)
{
sort(stones, stones + N);
for (int i = 0; i < N; i++)
{
int j = lower_bound(stones + i + 1, stones + N, stones[i] + D) - stones;
if (j < N && stones[j] == stones[i] + D)
return {i, j};
}
return {-1, -1};
}
```
时间复杂度为O(N),空间复杂度为O(N)的算法:
```
#include <unordered_set>
#include <vector>
using namespace std;
vector<int> findStonePair(double* stones, int N, double D)
{
unordered_set<double> st;
for (int i = 0; i < N; i++)
{
if (st.count(stones[i] - D))
return {i, distance(st.find(stones[i] - D), st.end()) - 1};
st.insert(stones[i]);
}
return {-1, -1};
}
```
问题C:以下是C++代码实现,时间复杂度为O(max(R, N)),空间复杂度为O(N)。
```
#include <unordered_set>
#include <unordered_map>
#include <vector>
using namespace std;
vector<vector<int>> findAllStonePairs(double* stones, int N, double D)
{
unordered_map<double, int> mp;
for (int i = 0; i < N; i++)
mp[stones[i]] = i;
unordered_set<int> visited;
vector<vector<int>> res;
for (int i = 0; i < N; i++)
{
if (visited.count(i))
continue;
visited.insert(i);
if (mp.count(stones[i] + D))
{
int j = mp[stones[i] + D];
visited.insert(j);
res.push_back({i, j});
}
else if (mp.count(stones[i] - D))
{
int j = mp[stones[i] - D];
visited.insert(j);
res.push_back({i, j});
}
}
return res;
}
```
测试程序:
```
#include <iostream>
int main()
{
double stones[] = {1.2, 3.4, 5.6, 7.8};
vector<int> res = findStonePair(stones, 4, 2.2);
cout << res[0] << " " << res[1] << endl;
vector<vector<int>> res2 = findAllStonePairs(stones, 4, 2.2);
for (auto& pair : res2)
cout << pair[0] << " " << pair[1] << endl;
return 0;
}
```
边界情况测试:
1. N=0或N=1,函数返回{-1,-1},不需要验证。
2. 存在多个满足条件的石头对,测试函数能否正确返回任意一对石头的下标。
3. 所有石头都相同,函数返回{-1,-1},验证函数是否能够正确处理。
4. D为0,函数能否正确返回一对重量相同的石头的下标。
5. 所有石头均为正数或负数,函数能否正确处理。
6. 所有石头均为0,函数能否正确处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)