C++ 的问题数论代码

发布于 2024-10-06 10:16:49 字数 976 浏览 4 评论 0原文

int sum(int number, int min, int counter)
{
    int temp=0, n;
    n=number+temp;
    if ((number>=(n/2)) & (number!=min))
    {
        number --;
        temp ++;
        while (number>=(n/2))
        {
            cout << number << "+"<< temp << "\n";
            number --;
            temp ++;
            counter ++;
        }
    }
    else if (number==1)
    {
        return counter;
    }

    sum(n-1, 1,counter);
}

int main()
{
    int number,counter=1;

    cout << "Please enter the number: ";
    cin >> number ;
    cout << "\n";

    sum(number, 1, counter);
    cout << counter;

    return 0;
}

做了我需要的事情,但崩溃了并且有一些问题。只是寻找一些关于如何改进它的技巧。

还想确保我正确地练习递归。谢谢!

添加: 该程序的要点是计算一个数字相加的最大可能方式。

那么总和为 6 的序列数为 11(包括 6 本身)。

6
5+1
4+1+1
3+1+1+1
2+1+1+1+1
1+1+1+1+1+1
2+2+1+1
3+2+1
4+2
2+2+2
3+3

我还尝试不重复序列,例如 2+2+1+1 和 1+1+2+2。

int sum(int number, int min, int counter)
{
    int temp=0, n;
    n=number+temp;
    if ((number>=(n/2)) & (number!=min))
    {
        number --;
        temp ++;
        while (number>=(n/2))
        {
            cout << number << "+"<< temp << "\n";
            number --;
            temp ++;
            counter ++;
        }
    }
    else if (number==1)
    {
        return counter;
    }

    sum(n-1, 1,counter);
}

int main()
{
    int number,counter=1;

    cout << "Please enter the number: ";
    cin >> number ;
    cout << "\n";

    sum(number, 1, counter);
    cout << counter;

    return 0;
}

Does what i need it to but crashes and has a few problems with it. Just looking for some tips on how t improve it.

Also wanted to make sure I'm practicing recursion correctly. Thanks!

ADD:
The point of the program is to total the max possible ways a number can be added up.

Then the number of sequences that sum up to 6 is 11 (including 6 itself).

6
5+1
4+1+1
3+1+1+1
2+1+1+1+1
1+1+1+1+1+1
2+2+1+1
3+2+1
4+2
2+2+2
3+3

I'm also trying not to have sequences that repeat, for example 2+2+1+1 and 1+1+2+2.

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

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

发布评论

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

评论(5

烟火散人牵绊 2024-10-13 10:16:49

你没有退出标准。你的函数将会递归到空中。

You have no exit criterion. Your function will just recurse into the skies.

屋檐 2024-10-13 10:16:49

正如我在问题中评论的那样,您的 sum 函数最终不会返回值。另一个问题是您将“计数器”作为值传递,因此它不会在主函数中更改。你应该像这样声明你的函数

int sum(int number, int min, int &counter)

:运算符确定“counter”是引用变量,可以通过“sum”函数更改。

正如 ruslik 评论的那样,您应该更改运算符 &进入 &&,尽管它在这里影响不大。

您可以在此处查看示例代码。

As I comment above in the question, your sum function doesn't return value at the end. Another problem is that you pass "counter" as a value, so it won't be changed in your main function. You should declare your function like this:

int sum(int number, int min, int &counter)

The & operator determine that "counter" is a reference variable, and can be changed by "sum" function.

And as ruslik comments, you should change operator & into &&, though it doesn't affect much here.

You can take a look at the sample code here.

蘑菇王子 2024-10-13 10:16:49

此链接将帮助您完成此处你去

修改了Hoàng Long的代码以达到期望的结果,有一个错误,它打印了一个额外的组合,但我懒得解决它。

这是 代码

编辑
纠正了错误。

This link will help you through here you go

Modified code from Hoàng Long to achieve the desired result there is one bug it prints one extra combination but I am too lazy to solve it.

Here is the code

EDIT
Corrected the bug.

倾城°AllureLove 2024-10-13 10:16:49

这是一个简单的实现:

void sum_r(int n, int m, int cnt, int* nums){
    for (;n >= m; m++)
        sum_r(n-m, nums[cnt] = m, cnt+1, nums);
    if (!n) for (int i=0; i<cnt; i++) printf("%d%c",nums[i],(i==cnt-1)?'\n':'+');
};

void sum(int n){
    int nums[100];
    return sum_r(n, 1, 0, nums);
};

int main(){
    sum(6);
    return 0;
};

Here is a simple implementation:

void sum_r(int n, int m, int cnt, int* nums){
    for (;n >= m; m++)
        sum_r(n-m, nums[cnt] = m, cnt+1, nums);
    if (!n) for (int i=0; i<cnt; i++) printf("%d%c",nums[i],(i==cnt-1)?'\n':'+');
};

void sum(int n){
    int nums[100];
    return sum_r(n, 1, 0, nums);
};

int main(){
    sum(6);
    return 0;
};
一笑百媚生 2024-10-13 10:16:49

请阅读《计算机编程艺术》第4A卷,Knuth已经清楚地列出了许多这样做的方法,例如,最简单的方法之一(无需递归)就是这样,

替代文字

Please read The art of computer programming volume 4A, Knuth has clearly listed many methods in doing that, for example, one of the simplest way (without recursion) is this,

alt text

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