简体   繁体   English

递归程序中的分段错误

[英]Segmentation fault in recursive program

I am doing the coin problem, the problem says that,我在做硬币问题,问题说,

Given a set of coin values coins = {c1, c2,..., ck} and a target sum of money n , our task is to form the sum n using as few coins as possible.给定一组硬币值coins = {c1, c2,..., ck}和目标金额n ,我们的任务是使用尽可能少的硬币形成总和n

suppose you have 9 dollars and you have set of {6,5,1} so, the minimum no.假设你有 9 美元并且你有一组{6,5,1}所以,最小没有。 of sum/change for 9 dollars would be ( 6+1+1+1=9) ie 4 . 9 美元的总和/零钱将是( 6+1+1+1=9)4

i tried doing it recursively using this formula:我尝试使用以下公式递归地执行此操作:

solve(x) = min( solve(x−6)+1, solve(x−5)+1, solve(x−1)+1 )

,but I don't know why I'm getting Segmentation fault in my code. ,但我不知道为什么我的代码中出现分段错误。

There are plenty of codes available online, but I want to know what am I doing wrong here, I'm new to recursion please help me, The code goes here:网上有很多代码,但我想知道我在这里做错了什么,我是递归新手,请帮助我,代码在这里:

//my code

#include<bits/stdc++.h>
using namespace std;
int solve (int x, int a[], int n)
{
  if (x < 0)
    {
      return INT_MAX;
    }
  if (x == 0)
    {
      return 0;
    }
  int best = INT_MAX;
  for (int i = 0; i < n; i++)
    {
      best = min (best, solve (x - a[i], a, n) + 1);
    }
  return best;
}

int main ()
{
int a[] = { 6, 5, 1 };
int x = 9;
int n = 3;
cout << solve (x, a, n);
  return 0;
}

The code which have been took from: https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/代码取自: https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/

    #include <iostream>
    using namespace std;
    
    int minCoins(int coins[], int m, int amount) {
        if (amount == 0) return 0;

        int res = INT_MAX;

        for (int i = 0; i < m; i++) {
            if (coins[i] <= amount) {
                int sub_res = minCoins(coins, m, amount - coins[i]);

                if (sub_res != INT_MAX && sub_res + 1 < res) { // avoid overflow 
                    res = sub_res + 1;
                }
            }
        }
        return res;
    }

    int main() {
        int coins[] = { 6, 5, 1 };
        int amount = 9;
        cout << "Min coins is " 
             << minCoins(coins, sizeof(coins) / sizeof(coins[0]), amount) 
             << endl;
        return 0;
    }

About the problem:关于问题:

  1. Your Segmentation fault comes from the line: best = min (best, solve (x - i, a, n) + 1);您的分段错误来自以下行: best = min (best, solve (x - i, a, n) + 1); The reason is: xi will always gives you the same value so if you are run the program without debugging, your program crashing.原因是: xi总是会给你相同的值,所以如果你在没有调试的情况下运行程序,你的程序就会崩溃。 So don't try to debug it because it will takes a lot of time to see this crashing.所以不要尝试调试它,因为看到这个崩溃需要很长时间。 For starters change to: best = min (best, solve (x - a[i], a, n) + 1);对于初学者更改为: best = min (best, solve (x - a[i], a, n) + 1); . .

  2. After fixing the section 1, the if case: if (x < 0) return INT_MAX;修复第1节后,if case: if (x < 0) return INT_MAX; will causes problem and will return always the same value, which is: -INT_MAX .将导致问题并且将始终返回相同的值,即: -INT_MAX So you need to check the "if cases" again.所以你需要再次检查“if case”。

  3. The algorithm you try to implement is not correct, see the pseudo-code of this algorithm:您尝试实现的算法不正确,请参见该算法的伪代码:

     minchange(M): if M = 0: return 0 v <- infinity for c in denominations <= M: v <- min { minchange(M - c) + 1, v } return v
  4. Better use: sizeof(a) / sizeof(a[0]) instead of int n = 3 .更好地使用: sizeof(a) / sizeof(a[0])而不是int n = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM