如何创建一个打印出数组中所有重复整数的函数?

时间:2016-09-13 04:48:28

标签: c++ arrays sorting

我一直在尝试创建一个函数,打印出在数组中多次表示的所有整数。以下是我用它来打印出第一个重复的整数(5),但我似乎无法编辑它来识别第二对(6)。

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

const int SIZE = 5;
struct Pair{int one,two;};
Pair get(int hand[]);

int main()
{
    int hand[SIZE] = {5,6,5,2,6}; // input
    Pair number = get(hand);
    cout << "Pairs: " << number.one << ' ' << number.two << endl; 
    // Should output: Pairs: 5 6
    return 0;
}

Pair get(int hand[])
{
    int a = 0, b = 0;
    for (int i=0;i<SIZE;i++)
    { 
        for (int j=0;j<SIZE;j++)
        {
            if (hand[i] == hand[j])
            {
                a = hand[i];
            }
        }
    }
    Pair temp = {a,b};
    return temp;
}

6 个答案:

答案 0 :(得分:0)

您的代码有三个问题:

  1. 您永远不会打印匹配数num
  2. 您应该打印return hand[i];或存储在输出容器中,而不是hand[i]。您应该在main()

  3. 中打印上述容器的内容(如果您使用它)
  4. 在任何情况下cout << printPairs(hand) << endl;都应根据第2页中的决定进行适当更改

答案 1 :(得分:0)

有点STL的帮助...

#include <iostream>
#include <algorithm>
using namespace std;


const int SIZE = 5;

int main()
{
    int hand[SIZE] = {5,6,5,2,6};
    auto it = hand;
    auto end = hand + SIZE;

    sort(it, end);
    it = adjacent_find(it, end);
    while (it != end)
    {
        cout << *it++ << " ";
        it = adjacent_find(it, end);
    }
    return 0;
}

您最好先对数字进行排序,然后使用相邻的查找来查看相等的相邻数字。复杂度O(n log n + n)= O(n log n)

答案 2 :(得分:0)

我认为使用以下代码即可开始。 在您的代码中,您始终将hand [0]与hand [0]进行比较,依此类推。 你正在做num++但不在任何地方使用它。

在我的下面代码中,我使用异常,但是,如果您知道输入序列,则可以返回-10而不是抛出异常。

#include <iostream>
#include <stdexcept>

using namespace std;

const int SIZE = 5;
int printPairs(int hand[],int start=0);

int main()
{
    int hand[SIZE] = {5,6,5,2,6};
    //printPairs(hand) ;

    try{
        int start=0;
        while(true){
            std::cout<<printPairs(hand,start)<<std::endl;
            start++;
        }
    }
    catch(std::exception& ex){
        std::cout<<ex.what()<<std::endl;
    }
    return 0;
}

int  printPairs(int hand[],int start)
{
    for (int i=start;i<SIZE;i++)
    {
        int num=0;
        for (int j=i+1;j<SIZE;j++)
        {
            if (hand[i]==hand[j])
            {
                num++;
                return hand[i];
            }
        }
        //return hand[i];
    }
    throw std::runtime_error("No pair found");
}

答案 3 :(得分:0)

与您的代码类似我做了一些修改

Ideone Solution For Your Problem

#include <iostream>
#include<algorithm>
using namespace std;

const int SIZE = 7;
int printPairs(int hand[]);
int duplicate[SIZE];

int main()
{
    int hand[SIZE] = {5,6,5,2,6,2,5};
    int count = printPairs(hand);
    for(int i=0;i<count;i++)
    cout<<duplicate[i]<<endl;
    return 0;
}

int printPairs(int hand[])
{
    int n=0;
    for (int i=0;i<SIZE;i++)
    {
        int num=0;
        for (int j=i+1;j<SIZE;j++)
        {
            if (hand[i]==hand[j])
            {
                int val =hand[i];
                int *end = duplicate+SIZE ;
                int* v = std::find(duplicate , duplicate+SIZE , val);
                if(*v==*end)
                duplicate[n++]=hand[i];
            }
        }

    }
       return n;
}

但在我的情况下,我将返回重复的数量并在数组中更新它。

答案 4 :(得分:0)

由于问题被标记为C++,因此请使用c++ STL中已有的工具来解决问题。可以使用std:map解决此问题。

std::map是键值数据结构。所以你可以做:

Key (map::first) = Integer Value occurring inside your array "hand".
Value (map::second) = number of times the corresponding Key occurred.

可运行代码: http://ideone.com/CPNCUu

#include <iostream>
#include <map>
using namespace std;


int main() 
{
    const int SIZE = 5;
    int hand[SIZE] = {5,6,5,2,6};
    map<int, int> db;
    int i;
    map<int, int>::iterator it;
    for(i = 0; i < SIZE; ++i)
    {
        it = db.find(hand[i]);
        if(it == db.end())
        {
            db[hand[i]] = 1;
        }
        else
        {
            it->second += 1;
        }    
    }

    for(it = db.begin(); it != db.end(); ++it)
    {
        if(it->second > 1)
        {
            cout << "Integer " << it->first << " occurs " << it->second << " times." << endl;       
        }
    }
}

答案 5 :(得分:0)

如果您不介意它们的打印顺序,那么您可以使用地图:

#include <map>
#include <iostream>

using namespace std;

int main()
{
    int hand[] = {5,6,5,2,6};
    map<int,int> histogram;

    for (size_t i=0; i<sizeof(hand)/sizeof(*hand); i++)
    {
        auto iterator = histogram.find(hand[i]);
        if (iterator == histogram.end())
            histogram.insert({hand[i],1});
        else
            iterator->second++;
    }

    for (const auto& entry : histogram)
    {
        if (entry.second > 1)
            cout << entry.first << ' ';
    }

    return 0;
}