编程之美 - 找出故障机器

给出一个数字的列表,是否能够快速的找出其中只出现一次的数字。

想法: 定义一个动态的数组,读一个数字,如果这个数字没出现在动态数组中,则将它放在数组中,如果数组中已经有了,则删除。 最后剩下的就是只出现了一次的数据。

#include <iostream>
#include <vector>

using namespace std;

void check(int value, vector<int> &list)
{
    for (vector<int>::iterator it = list.begin(); it != list.end(); it++) 
    {
        if (*it == value)
        {
            list.erase(it);
            return;
        }
    }

    list.push_back(value);
}

int execute(int* pData, int nLen)
{
    int nRet = 0;
    int i = 0;
    vector<int> list;

    if (pData == NULL)
        return 0;

    for (i = 0; i < nLen; i++)
    {
        check(pData[i], list);
    }

    nRet = list.size();
    for (vector<int>::iterator it = list.begin(); it != list.end(); it++) 
    {
        cout << *it << ", " <<;
    }
    cout << endl;

    return nRet;
}

void main()
{
    int i = 0;
    int test[] = {12345,5456,3234,5645,3495,5979,4392,5876849,
                  458684,20849,12,82853,538459,96,
                  12345,5456,3234,5645,3495,5979,4392,5876849,
                  458684,20849,12,82853,538459,96, 
                  10, 15};

    execute(test, 30);
    cin >> i;
}

你可能感兴趣的:(编程之美 - 找出故障机器)