gpt4 book ai didi

C:将矩形切割成正方形的算法

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:07 25 4
gpt4 key购买 nike

我试着做这个来自 Codewars 的问题集,并通过编译器运行我的代码。它表明存在段错误,但我不知道我哪里出错了。问题集如下:

下图给出了如何将给定的“真实”矩形切割成正方形的想法(“真实”矩形意味着两个维度不同)。

[Click here for picture of the drawing]

你能把这张图转化成算法吗?

你会得到两个维度——一个正整数长度(参数名为lng)一个正整数宽度(名为 wdth 的参数)。您将返回一个数组。

我的代码如下:

typedef struct Data Data;
struct Data {
int *array;
int sz;
};

Data* sqInRect(int lng, int wdth) {
Data* ptr = (Data*) malloc(sizeof(Data));
// if square length == square width
if(lng == wdth){
ptr->array = NULL;
ptr->sz = 0;
return 1;
}
//initialise variables
int rmdLength,rmdWidth,rmdSmallSquares,rmdLengthTimes,rmdWidthTimes,SIZE,temp=0;

//assign length to temporary rmd variable
rmdLength = lng;
//calc how many squares of side *wdth* inside rect
while((lng-wdth)>wdth){
rmdLength -= wdth;
rmdLengthTimes++;
}
rmdLength -= wdth;
rmdLengthTimes++;
//assign width to temporary rmdWidth variable
rmdWidth = wdth;
//calc how many squares of side *rmdWidth* inside rect (remaining area)
while(rmdWidth>rmdLength){
rmdWidth -= rmdLength;
rmdWidthTimes++;
}
rmdWidth -= rmdLength;
rmdWidthTimes++;
//calculate final number of remaining squares
rmdSmallSquares = rmdLength/rmdWidth;
//calculate SIZE
SIZE = rmdLengthTimes + rmdWidthTimes + rmdSmallSquares;
//declaration of array
int arr[SIZE];
//for loop to put square values in array
temp = rmdLengthTimes + rmdWidthTimes;
for(int i=0;i<rmdLengthTimes;i++){
arr[i] = wdth;
}
for(int j=rmdLengthTimes;j<temp;j++){
arr[j] = rmdLength;
}
for(int k=temp;k<SIZE;k++){
arr[k]= rmdWidth;
}
//get Data* ptr to store array AND size of that array
ptr->array = arr;
ptr->sz = SIZE;
return ptr;
}

如果有人能向我澄清我哪里出错了,我将不胜感激。我被困在这个问题上太久了。谢谢。

最佳答案

您在以下行中有很多未初始化的变量:

  int rmdLength,rmdWidth,rmdSmallSquares,rmdLengthTimes,rmdWidthTimes,SIZE,temp=0;

注意:上面的行不会将所有变量初始化为0。它只将 temp 初始化为 0

要将所有变量初始化为0,您必须先声明它们,然后像​​这样初始化它们:

int rmdLength, rmdWidth, rmdSmallSquares, rmdLengthTimes, rmdWidthTimes, SIZE, temp;  
rmdLength = rmdWidth = rmdSmallSquares = rmdLengthTimes = rmdWidthTimes = SIZE = temp = 0;

使用未初始化的变量会导致未定义的行为:
来自online cpp reference on Uninitialized variables :

The value in an uninitialized variable can be anything – it is unpredictable, and may be different every time the program is run. Reading the value of an uninitialized variable is undefined behaviour – which is always a bad idea. It has to be initialized with a value before you can use it.

此外,在下面的代码中,您使用未初始化的变量 rmdLengthTimes 作为遍历数组的检查:

 for(int i=0;i<rmdLengthTimes;i++){
arr[i] = wdth;
}

这将导致访问越界内存地址,这也是未定义的行为:

来自 wiki :

The behavior of some programming languages—most famously C and C++—is undefined in some cases. In the standards for these languages the semantics of certain operations is described as undefined. These cases typically represent unambiguous bugs in the code, for example indexing an array outside of its bounds.

因此在使用变量之前正确初始化所有变量将解决您的问题。

关于C:将矩形切割成正方形的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52027005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com