c++输入一个值,若与已有的值重复,则不能被加入
时间: 2023-07-31 14:10:57 浏览: 36
可以使用 STL 中的 set 来实现此功能,set 可以自动去重。
具体实现代码如下:
```c++
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> s;
int n;
while (cin >> n) {
if (s.count(n)) {
cout << "该值已存在,不能被加入" << endl;
} else {
s.insert(n);
cout << "加入成功" << endl;
}
}
return 0;
}
```
在上面的代码中,我们使用 set 来存储已有的值,每次输入一个新值时,首先判断该值是否已经存在于 set 中,如果存在,输出提示信息;如果不存在,将该值加入 set 中,并输出成功信息。
相关问题
C++输入一个字符串,字符串全部由大写英文字母组成、在字符串中’C、’S’、’P’是一个组合,不需要考虑组合顺序,且字符不可以重复使用。输出可以组合多少CSP的组合。
这个问题可以通过回溯法来解决。我们可以遍历字符串中的每个字符,然后对每个字符进行选择或者不选择。
具体的算法如下:
1. 初始化一个计数器`count`为0,表示CSP组合的数量。
2. 定义一个递归函数`backtrack`,该函数有三个参数:当前处理的位置`index`,当前已选择的字符集合`chosen`,字符串`s`。
3. 在递归函数中,首先判断如果`chosen`中包含了'C'、'S'和'P'这三个字符,那么说明找到了一个CSP组合,将计数器`count`加1。
4. 然后遍历从`index`开始到字符串末尾的每个字符,如果当前字符不在`chosen`中,那么将其加入`chosen`中,然后递归调用`backtrack`函数,处理下一个位置`index+1`。
5. 处理完当前位置后,需要将当前字符从`chosen`中移除,以便处理下一个字符。
6. 最后,返回计数器`count`的值作为结果。
下面是一个使用C++实现的示例代码:
```cpp
#include <iostream>
#include <unordered_set>
using namespace std;
int countCSP(string s) {
int count = 0;
unordered_set<char> chosen;
// 回溯函数
function<void(int, unordered_set<char>&)> backtrack = [&](int index, unordered_set<char>& chosen) {
if (chosen.size() == 3 && chosen.count('C') && chosen.count('S') && chosen.count('P')) {
count++;
}
for (int i = index; i < s.length(); i++) {
char c = s[i];
if (chosen.count(c) == 0) {
chosen.insert(c);
backtrack(i + 1, chosen);
chosen.erase(c);
}
}
};
backtrack(0, chosen);
return count;
}
int main() {
string s;
cout << "请输入由大写英文字母组成的字符串:";
cin >> s;
int result = countCSP(s);
cout << "可以组合的CSP的组合数量为:" << result << endl;
return 0;
}
```
你可以运行这段代码,并输入一个由大写英文字母组成的字符串,程序将输出可以组合的CSP的组合数量。
用c++实现任意给定一个有向图,设计一个算法,对它进行拓扑排序。拓扑排序算法思想:a.在有向图中任选一个没有前趋的顶点输出;b.从图中删除该顶点和所有以它为尾的弧;c.重复上述a、b,直到全部顶点都已输出,此时,顶点输出序列即为一个拓朴有序序列;或者直到图中没有无前趋的顶点为止,此情形表明有向图中存在环。
好的,我可以提供一个基于邻接表的C++实现,具体代码如下:
```c++
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// 邻接表中的节点
struct Node {
int val; // 节点的值
int indegree; // 节点的入度
vector<int> next; // 节点指向的下一个节点
Node(int val = 0, int indegree = 0): val(val), indegree(indegree) {}
};
// 拓扑排序函数
vector<int> topologicalSort(vector<Node>& graph) {
vector<int> res; // 存储拓扑排序结果
queue<int> q; // 存储入度为0的节点
for (auto& node : graph) {
if (node.indegree == 0) {
q.push(node.val);
}
}
while (!q.empty()) {
int cur = q.front();
q.pop();
res.push_back(cur);
for (auto& next : graph[cur].next) {
graph[next].indegree--; // 出度节点的入度减1
if (graph[next].indegree == 0) {
q.push(graph[next].val);
}
}
}
return res;
}
int main() {
int n, m;
cout << "请输入顶点数和边数:";
cin >> n >> m;
vector<Node> graph(n);
cout << "请输入每条边的起点和终点:" << endl;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
graph[u].next.push_back(v);
graph[v].indegree++;
}
vector<int> res = topologicalSort(graph);
if (res.size() == n) {
cout << "拓扑排序结果为:";
for (auto& val : res) {
cout << val << " ";
}
cout << endl;
} else {
cout << "该有向图存在环,无法进行拓扑排序!" << endl;
}
return 0;
}
```
上述代码中,我们使用了一个`Node`结构体来表示邻接表中的每个节点,其中包括节点的值、入度以及指向的下一个节点。在输入有向图信息时,我们根据每条边的起点和终点构建邻接表,并记录每个节点的入度。在进行拓扑排序时,我们将所有入度为0的节点加入队列,依次取出队列中的节点,并更新它指向的节点的入度,若更新后入度为0,则将这些节点加入队列。最终,如果拓扑排序结果中包含所有节点,则说明排序成功,否则说明存在环。