码迷,mamicode.com
首页 > 编程语言 > 详细

[算法]正整数划分

时间:2019-10-22 14:37:46      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:int   math   意思   http   hello   end   stat   static   std   

#include <iostream>
#include <cmath>

using namespace std;
//正整数划分
//最主要根据题目意思写出递归式
//未输出划分的具体过程
/*
6;
5+1;
4+2,4+1+1;
3+3,3+2+1,3+1+1+1;
2+2+2,2+2+1+1,2+1+1+1+1;
1+1+1+1+1+1
*/
static int q(int n, int m)
{
    if (n < 1 || m < 1)
    {
        return 0;
    }

    if (n==1 || m==1)
    {
        return 1;
    }

    if (n<m)
    {
        return q(n, n);
    }

    if (n==m)
    {
        return 1 + q(n, n - 1);
    }
    //n>m>1:
    return q(n, m - 1) + q(n - m, m);
}

int main()
{
    cout<<q(6, 6)<<endl;//11

    cout << "hello world" << endl;
    return 0;
}

技术图片

[算法]正整数划分

标签:int   math   意思   http   hello   end   stat   static   std   

原文地址:https://www.cnblogs.com/tailiang/p/11719443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!