什么是整数划分?
将正整数n表示成一系列正整数的和。
例如5的划分:
5
(1) 5;
(2) 4+1;
(3) 3+2 3+1+1;
(4) 2+2+1 2+1+1+1+1;
(5) 1+1+1+1+1;
p(5,4) = 6;
p(5,3) = 5;
p(5,2) = 3;
p(5,1) = 1;
解析:
q(num, m) = 1;
(num=1 or m=1)
q(num,m) = q(num, num); (num<m)
1+ q(num, m-1); (num=m)
q(num-m,m)+q(num,m-1); (num>m)
代码展示
import java.util.*;
import java.util.Scanner;
public class Q
{
public static void main(String[] args)
{
int num,m;
Scanner input = new Scanner(System.in);
System.out.println("请输入正整数以及最大划分数");
num = input.nextInt();
m = input.nextInt();
System.out.println(q(num,m));
}
static int q(int num,int m)
{
if( (num < 1) || (m < 1) ) return 0;
if( num == 1 || m == 1) return 1;
if(num < m) return q(num,num);
if( num == m) return q(num,m-1) +1;
return q(num,m-1)+q(num-m,m);
}
}
运行结果截图
网友评论