随机漫步 C语言测试题

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define false 0

int main()
{
    char in_box[10][10]={false};
    int num1,num2,num=0;
    int h=0,l=0,choice=0;
    int i,j;
    const char Alp[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
                'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    srand((unsigned) time(NULL));
    h=rand()%10;
    l=rand()%10;
    in_box[h][l]=Alp[num];
    num++;
    while((num<26)&&(choice<4))
    {
        num1=rand()%4;
        num2=rand()%4;
        switch(choice)
        {
            case 0:
            {
                if((!in_box[h+num1][l])&&(h+num1)<10)
                {
                    h+=num1;
                    in_box[h][l]=Alp[num];
                    num++;
                    choice=0;
                    //break;
                }
                else
                    choice++;
                break;
            }
            case 1:
            {
                if((!in_box[h-num1][l])&&(h-num1)>=0)
                {
                    h-=num1;
                    in_box[h][l]=Alp[num];
                    num++;
                    choice=0;
                    //break;
                }
                else
                    choice++;
                break;
            }
            case 2:
            {
                if((!in_box[h][l+num1])&&(l+num1)<10)
                {
                    l+=num1;
                    in_box[h][l]=Alp[num];
                    num++;
                    choice=0;
                    //break;
                }
                else
                    choice++;
                break;
            }
            case 3:
            {
                if((!in_box[h][l-num1])&&(l-num1)>=0)
                {
                    l-=num1;
                    in_box[h][l]=Alp[num];
                    num++;
                    choice=0;
                    //break;
                }
                else
                    choice++;
                break;
            }
        }
    }
	for(i=0;i<10;i++)
    	{
        	for(j=0;j<10;j++)
        	{
            		if(!in_box[i][j])
              	  	//printf("%c",'.');
             	  		printf('.');
          	 	else
              	  		printf("%c",in_box[i][j]);
        }
        printf("\n");
    }
    return 0;
}
x (x)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值