如何将字母A放在数组左上角第一个位置(随机分布)
编写程序,生成一种贯穿10*10字符数组的“随机分布”。A-Z程序代码:
#include <stdio.h> #include <time.h> #include <stdlib.h> #define MAXX 10 #define MAXY 10 int main() { int i =0, j = 0, count = 0; char map[MAXX][MAXY]; const char str='A'; for (i=0; i<MAXX; i++) { for (j=0; j<MAXY; j++) { map[i][j]='.'; } } srand( (unsigned) time(NULL) ); i = rand()%MAXX; j = rand()%MAXY; while(1) { switch (rand()%4) //0、1、2、3分别代表上、下、左、右 { case 0: if (i-1>=0 && map[i-1][j ]=='.') { map[i-1][j]=str+count; count ++; i -= 1; } break; case 1: if (i+1<=MAXX && map[i+1][j ]=='.') { map[i+1][j]=str+count; count ++; i += 1; } break; case 2: if (j-1>=0 && map[i ][j-1]=='.') { map[i][j-1]=str+count; count ++; j -= 1; } break; case 3: if (j+1<=MAXY && map[i ][j+1]=='.') { map[i][j+1]=str+count; count ++; j += 1; } break; } if(count ==26) break; } for(i=0;i<10;++i) { for(j=0;j<10;++j) printf("%c ",map[i][j]); printf("\n"); } return 0; }
搞了好久,求教