C语言扫雷基础

来源:互联网 发布:刚买的域名被墙 编辑:程序博客网 时间:2024/05/22 12:32
#include <stdio.h> #include <time.h>int main(){int arr[10][10]={0};//10*10=100个格子int row,col;//纵向与横向的循环变量int count=0;//统计随机地雷的数量srand((unsigned)time(NULL)); //随机种子,要配合随机数使用 do{row=rand()%10;col=rand()%10;if(arr[row][col]==0){arr[row][col]=-1;//标记为有地雷 count++;}}while(count<10); for(row=0;row<10;row++) {for(col=0;col<10;col++){if(arr[row][col]!=-1)//标记为没有地雷 printf("□");elseprintf("■");}printf("\n");} return 0;
利用两个for循环控制扫雷区域的大小,地雷的产生用随机数来控制,在使用rand之前,要设定随机种子,两者必须结合使用!
0 0
原创粉丝点击