02-方法 课程作业01:将课程作业01、02、03的设计思想、程序流程图、源程序代码和结果截图整理成一篇博文

一、计算组合数

  1、使用组合数公式利用n!来计算

    (1)设计思想:利用循环实现阶乘的计算,将阶乘封装为一个方法,在计算中调用该方法套入公式计算

    (2)程序流程图

    

    (3)程序源代码

 1 //信1605-1    刘思翔    20163579
 2 //递推计算组合数
 3 import java.util.Scanner;
 4 public class ditui 
 5 {
 6     public static int jiecheng(int x)    //递推方法计算阶乘
 7     {
 8         if(x==0)            //特殊值
 9         {
10             return 1;
11         }
12         else
13         {
14             int a=1;
15             for(int i = 1;i<=x;i++)
16             {
17                 a*=i;
18             }
19             return a;    
20         }
21     }
22     
23     public static int result(int n,int k)    //计算组合数
24     {
25         return (jiecheng(n))/((jiecheng(k)*(jiecheng(n-k))));
26     }
27     
28     public static void run()            //运行方法
29     {    
30         int n = 0,k = 0;
31         Scanner scanner= new Scanner(System.in);
32         while(true)
33         {
34             System.out.print("请输入组合数C(n,k)\nn = ");
35             n = scanner.nextInt();
36             System.out.print("k = ");
37             k = scanner.nextInt();
38             if(n>=k&&k>=0)
39             {
40                 System.out.println("C("+n+","+k+") = "+result(n,k));
41                 break;
42             }
43             else                        //判断输入的数值是否符合要求
44             {
45                 System.out.println("输入错误,请重新输入");
46             }
47         }
48     }
49     
50     public static void main (String args[])
51     {
52         run();
53     }
54 }

    (4)结果截图

      

  2、使用递推的方法用杨辉三角形计算

  

    (1)设计思想:用二维数组盛放杨辉三角,对其初始化为0,将第n行前n个数赋值为1,再套用公式进行计算

    (2)程序流程图

    

 

     (3)程序源代码

 1 //信1605-1    刘思翔    20163579
 2 //用杨辉三角计算组合数
 3 import java.util.Scanner;
 4 public class yanghui 
 5 {
 6     public static void main(String args[])
 7     {
 8         Scanner scanner = new Scanner(System.in);
 9         int n=0,k=0,x[][];                //n存储行数,二维数组x存储杨辉三角
10         while(true)
11         {
12             System.out.print("请输入组合数C(n,k)\nn = ");
13             n = scanner.nextInt();
14             System.out.print("k = ");
15             k = scanner.nextInt();
16             x = new int [n+1][n+1];
17             if(n>=k&&k>=0)
18             {
19                 for(int i=0;i<=n;i++)    //初始化
20                 {
21                     for(int j=0;j<=n;j++)
22                     {
23                         x[i][j] = 0;
24                     }
25                 }
26                 
27                 for(int i=0;i<=n;i++)    //将第i行的前i个数赋值为1
28                 {
29                     for(int j=0;j<=i;j++)
30                     {
31                         x[i][j] = 1;
32                     }
33                 }
34                 
35                 for(int i=1;i<=n;i++)    //递推计算
36                 {
37                     for(int j=1;j<=i;j++)
38                     {
39                         x[i][j] = x[i-1][j-1] + x[i-1][j];
40                     }
41                 }
42                 
43                 /*for(int i=0;i<=n;i++)    //打印杨辉三角
44                 {
45                     for(int j=0;j<=i;j++)
46                     {
47                         System.out.print(x[i][j]+"\t");
48                     }
49                     System.out.println();
50                 }*/
51                 System.out.println("C("+n+","+k+") = "+x[n][k]);
52                 break;
53             }
54             else                        //判断输入的数值是否符合要求
55             {
56                 System.out.println("输入错误,请重新输入");
57             }
58         }
59         
60         
61     }
62 }

    (4)结果截图

    

  3、使用递归的方法用组合数递推公式计算

(1)设计思想:利用递归的方法实现阶乘的计算,在计算中调用该方法套入公式计算

    (2)程序流程图

    

    (3)程序源代码

 1 //信1605-1班        刘思翔    20163579
 2 //用递归的方法计算组合数
 3 import java.util.Scanner;
 4 public class digui 
 5 {
 6     public static int jiecheng(int x)//递归方法计算阶乘
 7     {
 8         if(x==0)                    //特殊值
 9         {
10             return 1;
11         }
12         if(x==1)                    //终止条件    
13         {
14             return x;
15         }
16         else                        //递归调用
17         {
18             return x*jiecheng(x-1);
19         }
20     }
21     
22     public static int result(int n,int k)    //计算组合数
23     {
24         return (jiecheng(n))/((jiecheng(k)*(jiecheng(n-k))));
25     }
26     
27     public static void run()            //运行方法
28     {    
29         int n = 0,k = 0;
30         Scanner scanner= new Scanner(System.in);
31         while(true)
32         {
33             System.out.print("请输入组合数C(n,k)\nn = ");
34             n = scanner.nextInt();
35             System.out.print("k = ");
36             k = scanner.nextInt();
37             if(n>=k&&k>=0)
38             {
39                 System.out.println("C("+n+","+k+") = "+result(n,k));
40                 break;
41             }
42             else                        //判断输入的数值是否符合要求
43             {
44                 System.out.println("输入错误,请重新输入");
45             }
46         }
47     }
48     
49     public static void main (String args[])
50     {
51         run();
52     }
53 }

 (4)结果截图

      

二、递归编程解决汉诺塔问题

  (1)设计思想:运用递归,将之前每一次的移动看做一整次移动

  (2)程序流程图

   

  (3)程序源代码

 1 //信1605-1班        刘思翔    20163579
 2 //递归方法解决汉诺塔问题
 3 import java.util.Scanner;
 4 public class hannuota 
 5 {
 6     public static void main(String args[])
 7     {
 8         int n;
 9         Scanner scanner = new Scanner(System.in);
10         System.out.print("请输入汉诺塔的层数:");
11         n = scanner.nextInt();
12         tower(n,'A','B','C');
13     }
14     
15     public static void tower(int x,char A,char B,char C)
16     {
17         if(x==1)
18         {
19             System.out.println(A+"——>"+C);
20         }
21         else
22         {
23              tower(x-1,A,C,B);         //使用递归先把A塔最上面的n-1个盘子移动到B塔上,C为过渡塔
24              System.out.print(A+"——>"+C+"\n");       //把A塔中底下最大的圆盘,移动到C塔上
25              tower(x-1,B,A,C); 
26         }
27     }
28 }

 

  (4)结果截图

  

三、使用递归方式判断某个字串是否是回文 

  (1)设计思想:输入字符串,判断其长度,将对应位置的字符取出后作比较  

  (2)程序流程图

  

 

  (3)程序源代码

 1 //信1605-1    刘思翔    20163579
 2 //用递归方法判断回文字符串
 3 import java.util.Scanner;
 4 public class huiwen 
 5 {
 6     public static void main(String args[])
 7     {
 8         String x;
 9         int flag=1;                
10         Scanner scanner = new Scanner(System.in);
11         System.out.print("请输入一个字符串:");
12         x = scanner.nextLine();
13         for(int i = 0;i<=x.length()/2;i++)    
14         {
15             if(x.charAt(i)==x.charAt(x.length()-i-1))//用charAt方法取出字符串中的字符进行比较
16             {
17                 continue;
18             }
19             else
20             {
21                 flag = 0;
22                 break;
23             }
24         }
25         if(flag==0)                //输出结果
26         {
27             System.out.println(x+"不是回文字符串");
28         }
29         else
30         {
31             System.out.println(x+"是回文字符串");
32         }
33     }
34 }

  (4)结果截图

 

posted @ 2017-10-14 00:06  野生小码农  阅读(496)  评论(0编辑  收藏  举报