在数组中查找唯一的数字
Find unique numbers in array
好吧,我必须找到一个数组中有多少不同的数字。
例如,如果阵列为:1 9 4 5 8 3 1 3 5
输出应该是6,因为1,9,4,5,8,3是唯一的,1,3,5是重复的(不是唯一的)。
到目前为止,这是我的代码。。。。。思维不正常。
#include <iostream>
using namespace std;
int main() {
int r = 0, a[50], n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < j; k++) {
if (a[k] != a[j]) r++;
}
}
cout << r << endl;
return 0;
}
让我入党吧;)
您也可以使用哈希表:
#include <unordered_set>
#include <iostream>
int main() {
int a[] = { 1, 9, 4, 5, 8, 3, 1, 3, 5 };
const size_t len = sizeof(a) / sizeof(a[0]);
std::unordered_set<int> s(a, a + len);
std::cout << s.size() << std::endl;
return EXIT_SUCCESS;
}
这在这里并不重要,但对于大型阵列来说,这可能具有最佳性能。
如果最小元素和最大元素之间的差异相当小,那么你可以做得更快:
- 创建一个跨越最小和最大元素范围的
vector<bool>
(如果您在编译时知道数组元素,我建议您使用std::bitset
,但无论如何,您都可以使用模板元编程在编译时计算所有元素) - 对于输入数组的每个元素,在
vector<bool>
中设置相应的标志 - 完成后,只需计算
vector<bool>
中true
的数量即可
std::set
只包含唯一元素。
#include <set>
int main()
{
int a[] = { 1, 9, 4, 5, 8, 3, 1, 3, 5 };
std::set<int> sa(a, a + 9);
std::cout << sa.size() << std::endl;
}
这个怎么样?
#include <list>
int main()
{
int a[] = {1, 9, 4, 5, 8, 3, 1, 3, 5};
std::list<int> la(a, a+9);
la.sort();
la.unique();
std::cout << la.size() << std::endl;
return 0;
}
既然您已经说过不能使用标准库,必须使用循环,那么让我们尝试这个解决方案。
#include <iostream>
using namespace std; // you're a bad, bad boy!
int main()
{
int r = 0, a[50], n;
cout << "How many numbers will you input? ";
cin >> n;
if(n <= 0)
{
cout << "What? Put me in Coach. I'm ready! I can do this!" << endl;
return -1;
}
if(n > 50)
{
cout << "So many numbers! I... can't do this Coach!" << endl;
return -1;
}
cout << "OK... Enter your numbers now." << endl;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "Let's see... ";
// We could sort the list but that's a bit too much. We will choose the
// naive approach which is O(n^2), but that's OK. We're still learning!
for (int i = 0; i != n; i++)
{ // Go through the list once.
for (int j = 0; j != i; j++)
{ // And check if this number has already appeared in the list:
if((i != j) && (a[j] == a[i]))
{ // A duplicate number!
r++;
break;
}
}
}
cout << "I count " << n - r << " unique numbers!" << endl;
return 0;
}
我敦促不要将此代码作为家庭作业提交,至少不要在不理解的情况下提交。你只会对自己造成伤害,你的导师很可能会知道你无论如何都没有写:我以前是一名评分员,当某人的代码质量神奇地提高时,这是很明显的。
我认为增加r值的位置是不正确的
#include <iostream>
using namespace std;
int main()
{
int r=0,a[50],n;
cin >>n;
for(int i=0;i<n;i++)
{
cin >> a[i];
}
for (int j=0;j<n;j++)
{
bool flag = true;
for(int k=;k<j;k++)
{
if(a[k]!=a[j])
{
flag = false;
break;
}
}
if (true == flag)
{
r++;
}
}
cout << r << endl;
return 0;
}
然而,我的建议是使用更复杂的算法(这个算法有O(N^2))。
这应该有效,但它可能不是最佳解决方案。
#include <iostream>
using namespace std;
int main()
{
int a[50],n;
int uniqueNumbers; // this will be the total numbers entered and we will -- it
cin >>n;
uniqueNumbers = n;
for(int i=0;i<n;i++)
{
cin >> a[i];
}
for (int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
/*
the and clause below is what I think you were missing.
you were probably getting false positatives when j == k because a[1] will always == a[1] ;-)
*/
if((a[k] == a[j]) && (k!=j))
{ uniqueNumebers--; }
}
}
cout << uniqueNumbers << endl;
return 0;
}
我们可以在这个程序中使用C++STL向量。
int main()
{
int a[] = {1, 9, 4, 5, 8, 3, 1, 3, 5};
vector<int>v(a, a+9);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
cout<<v.size()<<endl;
return 0;
}
请试运行您的代码在外for循环中,对于每个元素,它在内循环中被计数不止一个。让我们说这个循环包含1,2,3,4.1……个元素。在第二次迭代和第三次迭代中,1被计数,因为1是1=2以及1=3
现在是解决方案的时候了!!
#include<iostream>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
ll arr[1000007]={0};
int main()
{
ios_base::sync_with_stdio(false);//used for fast i/o
ll n;cin>>n;
for(ll i=1;i<=n;i++)
cin>>arr[i];
sort(arr,arr+n);
ll cnt=0;
for(ll i=1;i<=n-1;i++)
{
if(arr[i+1]-arr[i]==0)
cnt++;
}
cout<<n-cnt<<endl;
cin.tie(NULL);
return 0;
}
没有使用set那么优雅,但的工作速度快1.4倍
int a[] = {1, 9, 4, 5, 8, 3, 1, 3, 5}
std::map<int, char> m;
for (int i = 0; i < 9; i++) {
if ( m.count(a[i]) == 0 )
m.insert( pair<int, char>(a[i], 0x00) );
}
映射m
中的键表示数组a
中唯一值的列表
#include<bits/stdc++.h>使用命名空间std;
int find_unique(int arr[], int size){
int ans = 0;
for(int i = 0; i < size; i++){
ans = ans^arr[i]; // this is bitwise operator .its call XOR it's return only unique value..
}
return ans;
}
void print_array(int arr[], int size){
for(int i = 0; i < size; i++){
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); // use for fast input and output....
int arr[5] = {1, 3, 5, 3, 1};
cout <<"Orginal array: " << endl;
print_array(arr, 5);
int result = find_unique(arr, 5);
cout << result << endl;
return 0;
}
相关文章:
- 按唯一标识符描述数字列表
- C++:创建1000次唯一的随机分布,在任何分布中都没有重复的数字
- 需要在C 中生成一个从00001到99999的唯一5位数字
- 生成跨系统唯一数字的算法
- 选择6个随机唯一数字
- 如何将目录路径转换为唯一的数字标识符 (Linux/C++)
- 标准容器/库,用于存储数字窗口并返回唯一数字的数量
- 在c++中随机生成n以内的唯一数字
- 连续获取 2 个唯一数字
- 测量数组中的唯一数字
- 在编译时生成唯一的数字
- 需要在数组中找到一个唯一的数字
- 如何使用std::find查找唯一的数字
- C++中的唯一数字
- 随机生成n个唯一的数字
- c++ reinterpret_cast,生成唯一的数字
- 进程间在0到255之间的唯一数字
- 从一组集合{{中选择一组数字{1,2,3}.},{.},{.}}使得所有元素都是唯一的
- 在数组中查找唯一的数字
- 通过类型标识符表示类的数字唯一标识符