我的帕斯卡三角形出了什么问题?

发布于 2024-12-21 09:16:40 字数 2033 浏览 1 评论 0原文

我最近一直在寻找一些简单的编码挑战,并发现了帕斯卡三角形(此处< /a>),我尝试用 C/Objective-C 自己生成一个。对于那些不知道它是什么的人,该链接很好地解释了它。

第四行之后我开始感到奇怪,我就是不明白为什么。

我的 5 次迭代的输出当前如下所示:

   1      
  1 1     
 1 2 1    
1 3 3 1   
 4 6 3 1

它应该如下所示:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

这是到目前为止我的代码。第一个循环只是一个重置循环(将所有值设置为 0)。实际逻辑主要发生在第二个循环中。第三个循环是将值连接并格式化为字符串。

我对这段代码的评论比我自己的评论要多得多,只是为了提高可读性。

int iterations, i, b, mid, chars, temp;
NSLog(@"Please enter the number of itereations");
scanf("%i",&iterations); // take users input and store it in iterations

// calculate where the first 1 should go.
if (iterations % 2 == 0) mid = (iterations)/2;
else mid = (iterations+1)/2;

chars = iterations*2;

int solutions[iterations][chars];

// reset loop
for (i = 0; i<iterations; i++) {
    for (b = 0; b<chars; b++) {
        solutions[i][b] = 0;
    }
}

solutions[0][mid] = 1; // place the initial 1 in first row

for (int row = 1; row<iterations; row++) {
    for (int chi = 0; chi<chars; chi++) {
        temp = 0;
        if (chi > 0) {
            temp += solutions[row-1][chi-1]; // add the one diagonally left
        }
        if (chi < iterations) {
            temp += solutions[row-1][chi+1]; // add the one diagonally right
        }
        solutions[row][chi] = temp; // set the value
    }
}

// printing below...

NSMutableString *result = [[NSMutableString alloc] initWithString:@"\n"];
NSMutableString *rowtmp;

for (i = 0; i<iterations; i++) {
    rowtmp = [NSMutableString stringWithString:@""];
    for (b = 0; b<chars; b++) {
        if (solutions[i][b] != 0) [rowtmp appendFormat:@"%i",solutions[i][b]];
        else [rowtmp appendString:@" "]; // replace any 0s with spaces.
    }
    [result appendFormat:@"%@\n",rowtmp];
}

NSLog(@"%@",result);
[result release];

我感觉问题可能与偏移有关,但我不知道如何解决它。如果有人能发现我的代码哪里出了问题,那就太好了。

I've been looking for some simple coding challenges recently, and discovered about Pascal's triangle (here), and I've tried to generate one myself in C/Objective-C. For those that don't know what it is, that link explains it pretty well.

I'm starting to get oddness after the fourth row, and I just can't figure out why.

My output for 5 iterations currently looks like this:

   1      
  1 1     
 1 2 1    
1 3 3 1   
 4 6 3 1

It should look like this:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Here is my code so far. The first loop is just a reset loop (setting all the values to 0). The actual logic happens mostly in the second loop. The third loop is where the values are concatenated and formatted in a string.

I've commented this code much more than I would for myself just to aid readability.

int iterations, i, b, mid, chars, temp;
NSLog(@"Please enter the number of itereations");
scanf("%i",&iterations); // take users input and store it in iterations

// calculate where the first 1 should go.
if (iterations % 2 == 0) mid = (iterations)/2;
else mid = (iterations+1)/2;

chars = iterations*2;

int solutions[iterations][chars];

// reset loop
for (i = 0; i<iterations; i++) {
    for (b = 0; b<chars; b++) {
        solutions[i][b] = 0;
    }
}

solutions[0][mid] = 1; // place the initial 1 in first row

for (int row = 1; row<iterations; row++) {
    for (int chi = 0; chi<chars; chi++) {
        temp = 0;
        if (chi > 0) {
            temp += solutions[row-1][chi-1]; // add the one diagonally left
        }
        if (chi < iterations) {
            temp += solutions[row-1][chi+1]; // add the one diagonally right
        }
        solutions[row][chi] = temp; // set the value
    }
}

// printing below...

NSMutableString *result = [[NSMutableString alloc] initWithString:@"\n"];
NSMutableString *rowtmp;

for (i = 0; i<iterations; i++) {
    rowtmp = [NSMutableString stringWithString:@""];
    for (b = 0; b<chars; b++) {
        if (solutions[i][b] != 0) [rowtmp appendFormat:@"%i",solutions[i][b]];
        else [rowtmp appendString:@" "]; // replace any 0s with spaces.
    }
    [result appendFormat:@"%@\n",rowtmp];
}

NSLog(@"%@",result);
[result release];

I have a feeling the problem may be to do with the offset, but I have no idea how to fix it. If anyone can spot where my code is going wrong, that would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(粗略地看)原来的中点计算似乎是不正确的。我认为它应该简单地是:

mid = iterations - 1;

在 5 次迭代的示例中,中点需要位于数组位置 4。每次迭代都会向左“移动”一个位置。然后,第二次迭代(第二行)将在位置 3 和 5 处放置 1。第三次迭代在 2 和 6 处。第四次迭代在 1 和 7 处。第五次和最后一次迭代将在 0 和 8 处填充 1。

另外,用于临时添加的第二个 if 语句应如下所示,否则它会读取超出数组边界的末尾:

if (chi < iterations - 1) {

It appears (from a brief look) that the original midpoint calculation is incorrect. I think it should simply be:

mid = iterations - 1;

In the example of 5 iterations, the midpoint needs to be at array position 4. Each iteration "moves" one more position to the left. The 2nd iteration (2nd row) would then place a 1 at positions 3 and 5. The 3rd iteration at 2 and 6. The 4th at 1 and 7. And the 5th and last iteration would fill in the 1s at 0 and 8.

Also, the second if statement for the temp addition should be as follows otherwise it reads past the end of the array bounds:

if (chi < iterations - 1) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文