给一副扑克牌,求最长的顺子

#include
#include
#include 
#include 

using namespace std;
using  std::random_shuffle;

struct Recv
{
    int difference;
    set<int>::iterator first;
    set<int>::iterator last;
};

void Sort(int *arr, int len)
{
    set<int>  myset(arr, arr + len);
    Recv recv = { 0,myset.begin(),myset.begin() };
    set<int>::iterator lt = myset.begin();
    set<int>::iterator st = myset.begin();
    if (st != myset.end() && (++st != myset.end()))
    {
        ;
    }
    int i = 1;
    for (; st != myset.end(); )
    {
        if ((*st - i) == *lt)
        {
            st++;
            i++;
        }
        else if (recv.difference < i)
        {
            recv.first = lt;
            recv.last = st;
            recv.difference = i;
            i = 1;
            lt = st;
            st++;
        }
        else
        {
            i = 1;
            lt = st;
            st++;
        }
    }
    for (set<int>::iterator mt = recv.first; mt != recv.last; ++mt)
    {
        cout << *mt << " ";
    }
    cout << "size=" << recv.difference << endl;
    cout << endl;
}

template<class T>//强大的仿函数
class display
{
public:
    void operator()(const T& x)
    {
        cout << x << ' ';
    }
};

void C()
{
    vector<int> ve;
    for (int i = 0; i < 20; ++i)
    {
        ve.push_back(i);
    }
    for_each(ve.begin(),ve.end(),display<int>());
    cout << endl;
    random_shuffle(ve.begin(),ve.end());
    for_each(ve.begin(), ve.end(), display<int>());
}

void main()
{
    //C();//忽略,只是为了练习一下random_shuffle函数
    int arr[] = { 9,3,2,1,8,2,2,4,1,5,6,11};
    int len = sizeof(arr) / sizeof(arr[0]);
    Sort(arr, len);
}

你可能感兴趣的:(c++)