举例如下:
//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
int main(void){
int a[7][7],t[50],i,j,k,x;
srand((unsigned)time(NULL));
for(i=0;i<50;t[i]=i++);
for(x=50,i=0;i<7;i++)
for(j=0;j<7;j++){
a[i][j]=t[k=rand()%x];
t[k]^=t[x];
t[x]^=t[k];
t[k]^=t[x];
if(--x==-1) x=50;
}
for(i=0;i<7;i++){
for(j=0;j<7;printf("%3d",a[i][j++]));
printf("\n");
}
return 0;
}
typedef struct
{
int x;
int y;
} BulletPos;
int bulletMap[9][9] = {0}; // 地图
int counter = 0; // 雷计数
BulletPos bullet[9] = {0}; // 雷坐标
srand((int)time(NULL));
for (counter = 0; counter < 9; )
{
int x = rand()%9;
int y = rand()%9;
if (bullet[x][y] == 0){
bullet[x][y] = -1; // 置雷于此,并记录雷的纵横坐标
bullet[counter].x = x;
bullet[counter].y = y;
++counter;
}
}