C语言 #include <stdio.h> #include <stdlib.h> #include <time.h> int Random() { int r; srand(rand()+time(NULL)); r=rand()%4+1; }//随机数生成 int* Jud(const char map[][10],int x,int y) { static int j[4]={1}; if(map[x][y-1]!='*'||y==0) j[0]=0;//left if(map[x][y+1]!='*'||y==9) j[1]=0;//right if(map[x-1][y]!='*'||x==0) j[2]=0;//up if(map[x+1][y]!='*'||x==9) j[3]=0;//down return j; }//执行判断 void Print(char map[][10]) { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) printf("%c ",map[i][j]); printf("\n"); } }//输出地图 int main() { char map[10][10]; int i,j,m;//x接受随机值 for(i=0;i<10;i++) { for(j=0;j<10;j++) map[i][j]='*'; }//初始化 map[0][0]='A';//第一个是A //Print(map); int x=0,y=0;//记录坐标 int end=1;//标志结束 char c='A'; while(1) { int* j; j=Jud(map,x,y); while(1) { int m=Random(); if(!j[0]&&!j[1]&&!j[2]&&!j[3]) end=0; break; if(!j[m-1]) continue; else break; } switch(m) { case 1: map[x][--y]=c++; break; case 2: map[x][++y]=c++; break; case 3: map[--x][y]=c++; break; case 4: map[++x][y]=c++; break; } if(end=0||c=='Z') Print(map); break; } return 0; }查错
时间: 2024-02-01 14:04:07 浏览: 30
在代码中,有几个问题需要修正:
1. 在 Random 函数中,虽然使用了 srand 函数进行随机数种子的初始化,但是在生成随机数时却没有调用 rand 函数,应该加上如下代码:
```
r = rand() % 4 + 1;
```
2. 在 Jud 函数中,虽然返回了 j 数组的指针,但是在函数内部没有对 j 数组进行初始化,应该加上如下代码:
```
for (int k = 0; k < 4; k++) {
j[k] = 1;
}
```
3. 在主函数中,while 循环的条件语句中,使用了错误的赋值操作符“=”,应该改为条件判断符“==”:
```
if (end == 0 || c == 'Z')
```
4. 在 while 循环中,判断语句的括号不正确,应该改为如下代码:
```
while (end && (j[0] || j[1] || j[2] || j[3]))
```
修改后的代码如下:
相关问题
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat src = imread("C:/Users/iris/Pictures/scenery.jpg"); Mat dst = Mat::zeros(src.size(), src.type()); int rows = src.rows, cols = src.cols; int offsets = 5, random_num = 0; srand((unsigned)time(NULL)); for (int y = 0; y < rows - offsets; ++y) { for (int x = 0; x < cols - offsets; ++x) { random_num = rand() % offsets; dst.at<Vec3b>(y, x)[0] = src.at<Vec3b>(y + random_num, x + random_num)[0]; dst.at<Vec3b>(y, x)[1] = src.at<Vec3b>(y + random_num, x + random_num)[1]; dst.at<Vec3b>(y, x)[2] = src.at<Vec3b>(y + random_num, x + random_num)[2]; } } imshow("src", src); imshow("dst", dst); waitKey(0); destroyAllWindows(); return 0;不用其他库用c语言实现
抱歉,您提供的代码是使用了OpenCV库的C++代码,而您要求使用C语言实现。在C语言中,没有现成的图像处理库,因此需要使用其他第三方库或手动编写图像处理算法来实现。以下是使用C语言手动编写的简单图像处理算法,可以实现对图像进行随机偏移的效果:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BMP_HEADER_SIZE 54
int main(void)
{
FILE *fp_in, *fp_out;
unsigned char header[BMP_HEADER_SIZE];
int width, height, bit_count, line_size, offset, random_num;
unsigned char *img_in, *img_out;
srand((unsigned)time(NULL));
fp_in = fopen("input.bmp", "rb");
fp_out = fopen("output.bmp", "wb");
if (fp_in == NULL || fp_out == NULL) {
printf("Failed to open files!\n");
return 1;
}
fread(header, sizeof(unsigned char), BMP_HEADER_SIZE, fp_in);
width = *(int *)&header[18];
height = *(int *)&header[22];
bit_count = *(short *)&header[28];
line_size = (bit_count * width + 31) / 32 * 4;
offset = line_size - bit_count / 8 * width;
img_in = (unsigned char *)malloc(sizeof(unsigned char) * height * line_size);
img_out = (unsigned char *)malloc(sizeof(unsigned char) * height * line_size);
fread(img_in, sizeof(unsigned char), height * line_size, fp_in);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
random_num = rand() % 5;
img_out[y * line_size + x * bit_count / 8 + 0] = img_in[(y + random_num) * line_size + (x + random_num) * bit_count / 8 + 0];
img_out[y * line_size + x * bit_count / 8 + 1] = img_in[(y + random_num) * line_size + (x + random_num) * bit_count / 8 + 1];
img_out[y * line_size + x * bit_count / 8 + 2] = img_in[(y + random_num) * line_size + (x + random_num) * bit_count / 8 + 2];
}
}
fwrite(header, sizeof(unsigned char), BMP_HEADER_SIZE, fp_out);
fwrite(img_out, sizeof(unsigned char), height * line_size, fp_out);
free(img_in);
free(img_out);
fclose(fp_in);
fclose(fp_out);
return 0;
}
```
该代码使用了BMP文件格式,可以将图片保存为BMP格式后进行处理。在代码中,首先读取BMP文件头信息,然后根据图片的宽度、高度和每像素位数计算出每行像素数据的字节数。接着使用随机数生成器生成每个像素的随机偏移量,然后将原图像中的像素进行偏移,输出到新的图像中。最后将新的图像保存为BMP格式。
需要注意的是,该算法仅为简单的示例代码,可能存在一些局限性和缺陷。在实际应用中,需要根据具体的需求和情况进行优化和改进。
设计一“真随机数”函数:int MyRandom(int n,int m); 功能:n<m,随机产生n~m之间的整数,要求每次调用返回一随机数,均匀分布在n~m之间,注意程序每次运行,其产生的随机数是不同的。 注: 真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率随机产生的,其结果是不可预测的,是不可见的。而C语言中的随机函数 rand()是按照一定算法模拟产生的,其结果是确定的,是可见的。我们可以这样认为这个可预见的结果其出现的概率是100%。所以用计算机随机函数所产生的“随机数”并不随机,是伪随机数。而本题要求为不可预见的真随机数。 附:随机函数实例 #include <stdio.h> #include <time.h> #include <stdlib.h> int main () { srand(time(NULL)); //随机函数初始化 提示:只需要埋一次种子即可 printf("%d\n",rand()%100); //产生一个0-99的随机数 return 0; }
非常感谢您的提醒和补充!实际上,C语言中的rand()函数生成的随机数是伪随机数,而非真正意义上的随机数。为了实现真随机数生成,我们需要利用物理随机数发生器或者网络上的真随机数服务。
以下是一个使用网络上的真随机数服务生成随机数的例子:
```c++
#include <stdio.h>
#include <curl/curl.h>
int get_random_number(int n, int m) {
CURL *curl;
CURLcode res;
int rand_num;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.random.org/integers/?num=1&min=0&max=1000000000&col=1&base=10&format=plain&rnd=new");
res = curl_easy_perform(curl);
if (res == CURLE_OK) {
sscanf(curl_easy_strerror(res), "%d", &rand_num);
}
curl_easy_cleanup(curl);
}
int range = m - n + 1; // 计算n~m之间的整数范围
rand_num = rand_num % range + n; // 映射到n~m之间的整数
return rand_num;
}
int main() {
int n = 1, m = 10;
int rand_num = get_random_number(n, m);
printf("Random number between %d and %d: %d\n", n, m, rand_num);
return 0;
}
```
在这个例子中,我们使用了网络上的真随机数服务(https://www.random.org/integers/)生成一个0到1000000000之间的随机整数,然后将其映射到n~m之间的整数。需要注意的是,这种方式需要联网,并且网络延迟可能会影响随机数的生成效率。