写一个能计算一个数字的卡塔兰数的java程序。卡塔兰数Cn:(2n的阶乘)/((n+1)的阶乘·n的阶乘)
具体解释参考维基百科.
当n = 0, 1, 2 ,3,4,5,.....时
C = 1, 1, 2, 5, 14, 42,.....阶乘:N的阶乘:1-当n=0或者1时 N的阶乘为1,2-当n>1时,N的阶乘=n·(n-1)的阶乘。
0! = 1
1! = 1
2! = 2 . 1 = 2
3! = 3 . 2 . 1 = 6
4! = 4 . 3 . 2 . 1 = 24 程序输出结果:
Which catalan number do you want to calculate (-1 to stop):
0
n = 0: catalan number is 1
Which catalan number do you want to calculate (-1 to stop):
1
n = 1: catalan number is 1
Which catalan number do you want to calculate (-1 to stop):
2
n = 2: catalan number is 2
Which catalan number do you want to calculate (-1 to stop):
3
n = 3: catalan number is 5
Which catalan number do you want to calculate (-1 to stop):
8
n = 8: catalan number is 1430
Which catalan number do you want to calculate (-1 to stop):
-3
Invalid input:
Valid input range is an input >= 0 Which catalan number do you want to calculate (-1 to stop):
-1
(program exits)
大致意思就是当你输入数字0时,显示n=0 卡塔兰数为1,以此类推当你输入数字3时,显示n=3 卡塔兰数为5. 程序需要有处理异常的能力,当你输入一个无效的数字时,需要提示无效信息。(注意当输入-1时,结束程序)程序需要编写2个文件,Catalan.java 和 CatalanDemo.java. 下面是老师给的框架:(程序中卡塔兰数的算法不清楚怎么算)
===========================================
/* Catalan.java
* Contains 2 methods factorial and cata
*
*
* catalan calculates the n-th catalan number
* factorial calculates the factorial recursively
* Receive: n
* Return: n-th catalan number
***********************************************************/
public class Catalan
{
private long factorial(int n){ // fill in code
} public long calCatalan(int n)
{
// fill in code
}}=============================================================
/*
* Calculates the value of a specific Catalan number
*
* Input: integer value
* Output: n-th Catalan number
********************************************************/public class CatalanDemo
{
public static void main(String[] args)
{
// insert code
}
}
具体解释参考维基百科.
当n = 0, 1, 2 ,3,4,5,.....时
C = 1, 1, 2, 5, 14, 42,.....阶乘:N的阶乘:1-当n=0或者1时 N的阶乘为1,2-当n>1时,N的阶乘=n·(n-1)的阶乘。
0! = 1
1! = 1
2! = 2 . 1 = 2
3! = 3 . 2 . 1 = 6
4! = 4 . 3 . 2 . 1 = 24 程序输出结果:
Which catalan number do you want to calculate (-1 to stop):
0
n = 0: catalan number is 1
Which catalan number do you want to calculate (-1 to stop):
1
n = 1: catalan number is 1
Which catalan number do you want to calculate (-1 to stop):
2
n = 2: catalan number is 2
Which catalan number do you want to calculate (-1 to stop):
3
n = 3: catalan number is 5
Which catalan number do you want to calculate (-1 to stop):
8
n = 8: catalan number is 1430
Which catalan number do you want to calculate (-1 to stop):
-3
Invalid input:
Valid input range is an input >= 0 Which catalan number do you want to calculate (-1 to stop):
-1
(program exits)
大致意思就是当你输入数字0时,显示n=0 卡塔兰数为1,以此类推当你输入数字3时,显示n=3 卡塔兰数为5. 程序需要有处理异常的能力,当你输入一个无效的数字时,需要提示无效信息。(注意当输入-1时,结束程序)程序需要编写2个文件,Catalan.java 和 CatalanDemo.java. 下面是老师给的框架:(程序中卡塔兰数的算法不清楚怎么算)
===========================================
/* Catalan.java
* Contains 2 methods factorial and cata
*
*
* catalan calculates the n-th catalan number
* factorial calculates the factorial recursively
* Receive: n
* Return: n-th catalan number
***********************************************************/
public class Catalan
{
private long factorial(int n){ // fill in code
} public long calCatalan(int n)
{
// fill in code
}}=============================================================
/*
* Calculates the value of a specific Catalan number
*
* Input: integer value
* Output: n-th Catalan number
********************************************************/public class CatalanDemo
{
public static void main(String[] args)
{
// insert code
}
}
http://hi.bccn.net/space-21499-do-blog-id-13454.html
-
http://www.cnitblog.com/luckydmz/archive/2009/03/13/17572.html
-
http://www.blogjava.net/zellux/archive/2007/09/17/145952.html
{
private long factorial(int n){
if(n <= 1) return 1;
return n*factorial(n - 1);
} public long calCatalan(int n)
{
return factorial(2*n)/(factorial(n +1)*factorial(n)) ;
}
} public class CatalanDemo
{
public static void main(String[] args)
{
// insert code //这个也要人帮写的话还是回家种田或者做点小买卖吧
}
} 楼主你还打算找工作么