超出帕斯卡范围

发布于 2024-10-14 13:52:45 字数 567 浏览 2 评论 0原文

procedure solve(liko_skaitmenu, rezultatas : integer);
    var i, j : integer; 
begin
    if (not baigti) and (liko_skaitmenu = 0) and (rezultatas = b) then
        begin
            for j := 1 to c do
                WriteLn(ats[j]);
            baigti := true;
        end

        else 
            for i := 1 to N do
            begin
                ats[liko_skaitmenu] := i;
                solve(liko_skaitmenu-1,rezultatas + a[i]);
            end; 
end;

所以我遇到了范围溢出错误,而且我不知道我真正超出范围的地方。我试图用这个函数做的是尝试找到 N 长度数组中等于 b 的 c 元素之和。请帮我。

procedure solve(liko_skaitmenu, rezultatas : integer);
    var i, j : integer; 
begin
    if (not baigti) and (liko_skaitmenu = 0) and (rezultatas = b) then
        begin
            for j := 1 to c do
                WriteLn(ats[j]);
            baigti := true;
        end

        else 
            for i := 1 to N do
            begin
                ats[liko_skaitmenu] := i;
                solve(liko_skaitmenu-1,rezultatas + a[i]);
            end; 
end;

So I'm getting range overrun error, and I don't see where I really got out of range. What I'm trying to do with this function is try to find sum of c elements in N length array that equals b. Please help me.

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

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

发布评论

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

评论(2

迷爱 2024-10-21 13:52:46
if (not baigti) and (liko_skaitmenu = 0) and (rezultatas = b) then

当 liko_skatimenu 为 0 时,有可能计算结果为 false,因为计算结果也取决于 rezultatas 和 baigti。如果下次继续,您将得到 ats[-1] := i;,这可能不是您想要的。我会把它改成这样:

if (liko_skaitmenu = 0) or ((not baigti) and (rezultatas = b)) then
if (not baigti) and (liko_skaitmenu = 0) and (rezultatas = b) then

There's the possibility that this evaluates to false when liko_skatimenu is 0, because the result of the evaluation depends on rezultatas and baigti too. If it proceeds next time you'll have ats[-1] := i;, which probably isn't what you wanted. I'd change it into something like:

if (liko_skaitmenu = 0) or ((not baigti) and (rezultatas = b)) then
栀梦 2024-10-21 13:52:46

这些代码使用了几个全局变量,这使得理解变得困难,并且您没有显示在调用过程之前如何初始化变量。如果代码示例是英文的也会有帮助。

无论如何,

  1. 代码并不能防止 rezultatas > 的可能性。 b.
  2. 由于 if 上的复杂条件,ats[liko_skaitmenu] := i; 可能会以 liko_skaitmenu liko_skaitmenu 的值执行。 1..
  3. 该代码不防止重复相同的数字/索引位置。

您可能想要更多类似的东西:

if not baigti and (resultatas <= b) then (* if not told to stop, or off-range *)
begin
    if liko_skaitemu = 0 then
    begin
        (* finished searching: either success or failure *)
        if resultatas = b then
           (*success! save the values *)
           baigti := true;
        end;
    end
    else
    begin
       (* continue searching *)
    end
end;

也就是说,该方法是 O(N^c)。您可以通过对数组进行排序并将递归步骤限制为数组中可以保存答案的部分来做得更好,或者您可以使用数组中 c 个数字的组合。有 许多类似的问题在这个论坛上都有很好的答案。

The codes uses several global variables that make it difficult to understand, and you don't show how the variables are initialized before the procedure is called. It would also help if the code sample was in English.

Any way,

  1. The code doesn't guard against the possibility of rezultatas > b.
  2. Because of the complex condition on the if, the ats[liko_skaitmenu] := i; may be executed with values of liko_skaitmenu < 1.
  3. The code doesn't guard against repeating the same number/index position.

You probably want something more like:

if not baigti and (resultatas <= b) then (* if not told to stop, or off-range *)
begin
    if liko_skaitemu = 0 then
    begin
        (* finished searching: either success or failure *)
        if resultatas = b then
           (*success! save the values *)
           baigti := true;
        end;
    end
    else
    begin
       (* continue searching *)
    end
end;

That said, the approach is O(N^c). You can do better than that by sorting the array and restricting the recursive step to parts of the array that can hold the answer, or you can work with the combinations of c numbers in the array. There are many alike questions with good answers in this forum.

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