不同的加法模式等于一个固定的数字

huangapple go评论126阅读模式
英文:

Different modes of addition equal to a fixed number

问题

我正在尝试编写一个程序,该程序以n作为输入,并使用值数组numbers,编写等于n的和。我希望值元素的数量小于5。

我在递归函数中编写了这个:

int abjad(int n, int values[]) {
    if (n < 1) {
        return 0;
    } else {
        for (int i = 0; i < 20; i++) {
            if (n == values[i]) {
                printf("%d ", n);
            }
            abjad(n - values[i], values);
        }
    }
    return 0;
}
int main() {
    int values[] = {1, 2, 3, 4, 5, 6, 7, 9, 10, 20, 30, 40, 50, 60, 70, 80, 100, 200, 300, 600};
    int max_len = 5;
    int n;
    scanf("%d", &n);
    abjad(n, values);
    return 0;
}

如果n = 3,输出为:

1 2 1 3

但正确的输出应该是:

1 1 1
1 2
2 1
3
英文:

I am trying to write a program that takes the number n as an input and Using values array numbers, write sums that equal n. I want the number of values element less than 5.

i write this in recursive function

int abjad (int n ,int values[]){
    if (n&lt;1){
        return 0 ;
    }
    else {
        for (int i = 0 ; i&lt;20; i++){
            if(n==values[i]){
                printf(&quot;%d &quot;,n) ;
            }
            abjad(n-values[i],values) ;   
        }
    }    
    return 0 ;
}
int main() {
    int values[] = {1,2,3,4,5,6,7,9,10,20,30,40,50,60,70,80,100,200,300,600} ; 
    int max_len = 5 ;
    int n ;
    scanf(&quot;%d&quot;,&amp;n);
    abjad(n,values) ;
   return 0 ;
}

if n = 3 output:

1 2 1 3 

but true output is:

1 1 1
1 2 
2 1
3

答案1

得分: 1

以下是您要翻译的代码部分:

#include &lt;stdio.h&gt;

#define arraySize(...) (sizeof(__VA_ARGS__) / sizeof(*__VA_ARGS__))

int const values[] = {
        1, 2, 3, 4, 5, 6, 7, 9,
        10, 20, 30, 40, 50, 60, 70, 80,
        100, 200, 300, 600,
    };
int selected[5], nbSelected = 0;

void abjad(int n)
{
    if (n == 0) {
        for (int i = 0; i &lt; nbSelected; ++i) {
            printf(&quot;%d &quot;, selected[i]);
        }
        printf(&quot;\n&quot;);
    } else if (nbSelected &lt; arraySize(selected)) {
        for (int i = 0; i &lt; arraySize(values); ++i) {
            selected[nbSelected] = values[i];
            ++nbSelected;
            abjad(n - values[i]);
            --nbSelected;
        }
    }
}

int main()
{
    int n;
    scanf(&quot;%d&quot;, &amp;n);
    abjad(n);
}

希望这能满足您的要求。

英文:

I think this is what you’re looking for:

#include &lt;stdio.h&gt;

#define arraySize(...) (sizeof(__VA_ARGS__) / sizeof(*__VA_ARGS__))

int const values[] = {
        1, 2, 3, 4, 5, 6, 7, 9,
        10, 20, 30, 40, 50, 60, 70, 80,
        100, 200, 300, 600,
    };
int selected[5], nbSelected = 0;

void abjad(int n)
{
    if (n == 0) {
        for (int i = 0; i &lt; nbSelected; ++i) {
            printf(&quot;%d &quot;, selected[i]);
        }
        printf(&quot;\n&quot;);
    } else if (nbSelected &lt; arraySize(selected)) {
        for (int i = 0; i &lt; arraySize(values); ++i) {
            selected[nbSelected] = values[i];
            ++nbSelected;
            abjad(n - values[i]);
            --nbSelected;
        }
    }
}

int main()
{
    int n;
    scanf(&quot;%d&quot;, &amp;n);
    abjad(n);
}

It reminds me some exercises on recursivity on France-IOI.org.

I’m adding explanations in reaction to user4581301’s comment to my answer. Indeed being a Cargo Cult programmer is a nonsense for me; if you want to learn programming, you should start with a platform such as France-IOI (this one is quite good but is in French only). I tend to think that my codes are structured so that they only deal with a specific problem, and if you don’t understand them, you won’t get much out of them. Understanding my code is the fastest way to get a lot of things from them. And even if I add explanations, if you want to Cargo Cult program, you may still Cargo Cult program… Moreover, the code here is just so simple…

Here is how I have understood the problem (as the original formulation wasn’t clear): you want to get the lists of terms whose sum is equal to a specific number. But you don’t want any list to have a size greater than 5. Your terms are selected among an existing list of values (any term may be selected more than once, and the same values in a different order count as 2 different lists).

You weren’t so far from the solution: you accept a term, then search recursively which term could follow. The first thing to understand is that you must not show each term you select, because you might end in a situation in which you won’t finally build a correct list (because you’ve reached the limit of 5, or because the smallest possible term is greater than n while n is still greater than 0), and the term you select might actually be used for several lists if there are several solutions in the recursive call.

Therefore what you need to do is to use a stack. Many people just write:

selected[nbSelected++] = values[i];

to add a value to a stack, but I don’t like combining instructions like that (and I never use postfix operators, whose purpose is not very well known). When n reaches 0, you just have to print the content of your stack. Otherwise, if your stack is full, then building a list has failed.

The only thing you may have to concentrate on is this idea to combine a stack and recursive calls. In fact, recursive calls form a stack too. But you must put in a data structure what you’ve built in each execution of your function, otherwise child calls cannot know what has been done by previous executions and they cannot show the content of the list.

(France-IOI offers a code quite close to mine above as a solution to the problem “Choix des cours” in the chapter “Récursivité avancée”.)

huangapple
  • 本文由 发表于 2023年4月7日 03:48:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定