5

好吧,我必须找出一个数组中有多少个不同的数字。

例如,如果数组是: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;
}
4

8 回答 8

13

让我加入派对;)

您还可以使用哈希表:

#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>跨越 min 和 max 元素之间的范围的元素(如果您在编译时知道数组元素,我建议std::bitset您改为使用模板元编程来计算编译时的所有内容)。
  • 对于输入数组的每个元素,在 中设置相应的标志vector<bool>
  • 完成后,只需计算true.vector<bool>
于 2013-02-06T23:49:10.533 回答
10

Astd::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;
}
于 2013-02-06T23:38:29.087 回答
4

这个怎么样?

#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;
}
于 2013-02-06T23:35:29.967 回答
1

既然您已经声明您不能使用标准库并且必须使用循环,那么让我们试试这个解决方案。

#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;
}

敦促不要将此代码作为您的作业提交 - 至少不要在不理解它的情况下提交。你只会伤害自己,而且你的导师很可能会知道你并没有写它:我以前做过评分员,当某人的代码质量神奇地提高时,这是相当明显的。

于 2013-02-07T00:13:10.817 回答
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))。

于 2013-02-06T23:39:51.220 回答
0

这应该可行,但它可能不是最佳解决方案。

#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;
}
于 2013-02-06T23:56:18.150 回答
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;
}
于 2018-02-25T01:47:34.807 回答
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;
  }
于 2017-03-09T05:43:01.253 回答