neoz
2014-06-22 01:51:48 +08:00
<code>
#include<iostream>
#include<random>
#include<time.h>
using namespace std;
int rpc(int x[13],int xx,int ra)
{
if(xx>5)
{
if((ra-x[xx-1]==1)&&(x[xx-1]-x[xx-2]==1)&&(x[xx-2]-x[xx-3]==1)&&(x[xx-3]-x[xx-4]==1)&&(x[xx-4]-x[xx-5]==1))
return 1;
else
return 0;
}
return 0;
}
int main()
{
clock_t start_time=clock();
int count=0;
for(int j=0;j<10000;j++)
{
//random_device rd;
srand( (unsigned)time( NULL ) );
bool repeat[31]={0};
int x[13]={0};
int r=rand()%30+1;
x[0]=r;
repeat[r]=1;
for(int i=1;i<13;i++)
{
int ran=rand()%30+1;
while(repeat[ran]==0)
{
ran=rand()%30+1;
count++;
}
if(abs(ran-x[i-1])>5 || rpc(x,i,ran))
{
i--;
}
else
{
x[i]=ran;
repeat[ran]=1;
}
}
//cout<<j<<"--";
//for(int i=0;i<13;i++)
//cout<<x[i]<<" ";
//cout<<endl;
}
clock_t end_time=clock();
cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间
cout<<"The count of ceash is: "<<count<<endl;
return 0;
}
</code>
测了下是15-25ms之间。用未排序下去做。。。c++初学者。。。不会位运算,第一次想别人的问题,第一次贴出代码。还需努力,见笑了。。。