wm64195135 于 2014.11.29 12:25 提问

1个回答

hydirl   2014.12.03 17:23

#include "stdafx.h"
#include
#include
#include

#define N 10
int num[N];

int MyRand()
{
// srand(time(0));
return rand();
}

void Upset()
{
int tmp;
int a,b;
srand(time(0));
for(int j=0; j<N; j++)
{
for(int i=0; i<N; i++)
{
a = MyRand()%N;
b = MyRand()%N;
tmp = num[a];
num[a] = num[b];
num[b] = tmp;
}
}
}

void MonkeySequ()
{
srand(time(0));
int a,b,c,tmp;
a = MyRand()%N;
do{
b=MyRand()%N;
}while(a == b);
do{
c=MyRand()%N;
}while(a==c || b==c);

``````int type = MyRand()%6;
switch (type)
{
case 0:
tmp = num[a];
num[a] = num[b];
num[b] = tmp;
break;
case 1:
tmp = num[a];
num[a] = num[c];
num[c] = tmp;
break;
case 2:
tmp = num[c];
num[c] = num[b];
num[b] = tmp;
break;
case 3:
tmp = num[a];
num[a] = num[b];
num[b] = num[c];
num[c] = tmp;
break;
case 4:
tmp = num[a];
num[a] = num[c];
num[c] = num[b];
num[b] = tmp;
break;
case 5:
break;
}
``````

}

bool isRight()
{
for(int i=0; i<N; i++)
{
if(num[i] != i+1)
{
return false;
}
}

``````return true;
``````

}
int _tmain(int argc, _TCHAR* argv[])
{

``````for(int i=0; i<N; i++)
{
num[i] = i + 1;
}

Upset();
int count = 0;
while(true)
{
MonkeySequ();

for(int i=0; i<N; i++)
{
printf("%d ",num[i]);
}
printf("         (%d)\n",count++);

if(isRight())
{
break;
}

}
return 0;
}

vs2010 下的
``````