MR.yan's homework

#include <vector>

#include <iostream>

#include <ctime>

#include <cstdlib>

using std::vector;

using std::cout;

int main()

{

    int i,ran_num;

    vector<int>a,b;

    a.resize(10);

    b.resize(10);

    srand((unsigned)time(NULL));//create the time seed for rand

    for(i=0;i<10;i++) //clear the vector

    {

        a[i]=b[i]=-1;

    }

    for(i=0;i<10;i++)

    {ran_num=rand() % 10;

        if(a[i]==-1 )

        {

            if(a[ran_num]!=-1 || ran_num==i)

            {

                i--;

                continue;

            }

         a[i]=ran_num;

         a[ran_num]=i;//对应关系

        }



    }

    //这1步完成以后a[n]=m,则说明第n+1个人和第m+1个人是一对的(我们可以确保a[m]=n)

    for(i=0;i<10;i++)

    {ran_num=rand() % 10;

        if(b[i]==-1)

        {

            if(b[ran_num]!=-1)

            {

                i--;

                continue;

            }

            bool xt=false;//相同

            do

            {

                if(a[i]==ran_num)

                {

                    xt=true;

                    ran_num=rand() % 10;

                }

                else

                {

                    b[i]=ran_num;

                    b[ran_num]=i;

                }



            }while(xt);

        }



    }

    for(i=0;i<10;i++)

    {

        cout<<a[i]<<' ';

    }

    cout<<"------------\n";

    for(i=0;i<10;i++)

    {

        cout<<b[i]<<' ';

    }

    return 0;

}

 

你可能感兴趣的:(home)