C++ 的问题程序输出

发布于 2024-10-07 02:24:53 字数 1472 浏览 3 评论 0原文

Example: Let’s say your user input is 6.

Then the number of sequences that sum up to 6 is 11 (including 6 itself). This is shown clearly below:

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

You SHOULD NOT have any sequences that repeat. You cannot have 2+2+1+1 and 1+1+2+2 as two different combinations!!

代码:

#include <iostream>

using namespace std;

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

    sum(n-min, 1,counter);

    return 0;
}

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


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

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

    return 0;
}

我的输出是

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
3+1+2
2+3+1
4+2
2+2+2
3+3
0+1

Total out is 13. 

真实输出,对于那些不喜欢上面发布的内容的人来说,这是一个较短的版本。

5+1
4+2
3+3
4+1
3+2
2+3
3+1
2+2
2+1
1+2
1+1
0+1

13

Where 1+2 and 2+3 are doubles as listed above.

任何想法这里有什么问题吗?

Example: Let’s say your user input is 6.

Then the number of sequences that sum up to 6 is 11 (including 6 itself). This is shown clearly below:

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

You SHOULD NOT have any sequences that repeat. You cannot have 2+2+1+1 and 1+1+2+2 as two different combinations!!

CODE:

#include <iostream>

using namespace std;

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

    sum(n-min, 1,counter);

    return 0;
}

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


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

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

    return 0;
}

My output is

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
3+1+2
2+3+1
4+2
2+2+2
3+3
0+1

Total out is 13. 

Real output which is a shorter version for those of you who dont like whats posted above.

5+1
4+2
3+3
4+1
3+2
2+3
3+1
2+2
2+1
1+2
1+1
0+1

13

Where 1+2 and 2+3 are doubles as listed above.

Any ideas what is wrong here?

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

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

发布评论

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

评论(4

余生再见 2024-10-14 02:24:53

我想,如果您进行求和,以便第一个被加数始终尽可能高,并且不允许两个相邻的被加数中的第二个被加数大于第一个被加数,那么会更容易。

只是一个想法...

I guess it would be easier if you'd sum so that the first summand is always highest possible and you don't allow that of two adjacent summands the second one is greater than the first one.

Just a thought...

尹雨沫 2024-10-14 02:24:53

我已经在您之前的问题中发布了解决方案:

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;
};

编辑:我会尽力更好地解释它。主要思想是对生成的序列施加顺序,这将有助于避免重复。
为此,我们将使用 min 参数,它将是我们从现在开始在序列中可以使用的最小可能术语。
函数 sum_r 只是在每个递归级别打印 min 的值序列。
num 术语用作一种累加器,或“备用”的值。

我们可以编写一个更简单的函数,只计算此类序列的数量:

int sum_c(int n, int m){ 
    if (!n) return 1; // termination condition. end of sequence reached with "perfect match". this means we have found 1 additional sequence. Note that it's the only way of adding new values to result.
    int comb_cnt = 0;
    while (n >= m) { // we need a stop condition, and there is no point in having negative value of (n - m)
         comb_cnt +=   // here we accumulate all the solutions from next levels
            sum_c(n-m, m); // how many sequences are for current value of min?
         m++; // trying a larger `min`
    };
    return comb_cnt; // number of sequence fond at this level
};

I've already posted a solution to it in your previous question:

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;
};

EDIT: I'll try to explain it better. The main idea is to impose an order on the generated sequence, it will help in avoiding repetition.
We will use the min parameter for that, it will be the smallest possible term we can use from now on in the sequence.
The function sum_r just prints the sequence of values of min at each recursion level.
The num term is used as a kind of accumulator, or the value left "to spare".

We can write a simplier function, that just counts the number of such sequences:

int sum_c(int n, int m){ 
    if (!n) return 1; // termination condition. end of sequence reached with "perfect match". this means we have found 1 additional sequence. Note that it's the only way of adding new values to result.
    int comb_cnt = 0;
    while (n >= m) { // we need a stop condition, and there is no point in having negative value of (n - m)
         comb_cnt +=   // here we accumulate all the solutions from next levels
            sum_c(n-m, m); // how many sequences are for current value of min?
         m++; // trying a larger `min`
    };
    return comb_cnt; // number of sequence fond at this level
};
辞旧 2024-10-14 02:24:53

这里有一个提示:问题是计算分区输入号码。另请参阅:分区函数

Here's a hint: The problem is to compute the partitions of the input number. See also: partition function

与他有关 2024-10-14 02:24:53

C++ 中的逻辑 AND 运算符是 &&,而不是您在这一行中的 &

if ((number>=(n/2)) & (number!=0))

Well the logical AND operator in C++ is &&, not & as you have in this line:

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