Random Walk.c(随机数)

随机漫步

题目:1


#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;
}

本题思路:利用随机数,先随机选一个起始位置,然后上下左右依次移动,逐个排除


1.rand()函数

(1)作用:c语言中,产生随机数
(2)使用:

num1=rand()%4;
num2=rand()%4;

产生一个[0,4)的数;产生到那里为止的数就在%后接什么
若是

num1=rand()%4+1;

则最小值为1

2.srand()函数

(1)作用:随机数发生器的初始化函数
为了使rand()产生的随机数随机化,一般用时间做种子

srand((unsigned)time(NULL));

作用:获取系统当前时间

4.头文件

#include<time.h>

当有time()函数时使用

5.声明

error: ‘false’ undeclared (first use in this function)
也没有bool类型

原因是:c99以前没有定义bool,false,true;
需要自己定义
像这样:

#define bool int
#define true 1
#define false 0

c99中bool类型

#include<stdbool.h>

即可

6.break用法

(1)作用:跳出循环体(for循环,while循环,do…while循环)或者switch(只能是这两个)
(2)注意:break只能跳出离他最近的那个循环(一次一个循环)

     	case 0:
            {
                if((!in_box[h+num1][l])&&(h+num1)<10)
                {
                    h+=num1;
                    in_box[h][l]=Alp[num];
                    num++;
                    choice=0;
                    //break;//(1)
                }
                else
                    choice++;
                break;//(2)
            }

(1)处,对if语句不起作用
(2)处,跳出switch循环

补:
continue:(1)结束本次循环,即跳过循环体中下面尚未执行的语句,然后进行下一次是否执行循环的判定。
注意:只能在循环体(for循环,while循环,do…while循环)中使用,不能再switch中使用

7.warning: assignment makes pointer from integer without a cast

在源文件中没有发现函数的声明,可能是忘加头文件了。
注意:赋值和变量类型定义的不一样

8.输出小数点

错误示范:

printf('.');

然后编译器就会告诉你:
warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
printf(’.’);

正确:

printf("%c",'.');

猜你喜欢

转载自blog.csdn.net/weixin_45454859/article/details/102751640
今日推荐