课程作业02

Posted 肥鹅PU火

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课程作业02相关的知识,希望对你有一定的参考价值。

 

一、

1.设计思想

输入n,k;设计求n的阶乘函数,调用求出组合数;

2.流程图

 

 

 

3.代码

   package one;

 

import javax.swing.JOptionPane;

public class one {

 

      public static void main(String[] args) {

             // TODO 自动生成的方法存根

             String num1,num2;

             int n,k;

             num1=JOptionPane.showInputDialog( "请输入n:" );

             n=Integer.parseInt( num1 );

             num2=JOptionPane.showInputDialog( "请输入k:" );

             k=Integer.parseInt( num2 );

             zuhe(n, k);

             JOptionPane.showConfirmDialog(null, zuhe(n, k),"组合", JOptionPane.CLOSED_OPTION);

      }

     

public static int zuhe(int n,int k){

      int p;

      p=jiecheng(n)/(jiecheng(k)*jiecheng(n-k));

     

      return p;

}

public static int jiecheng(int n) {

     

      if(n==1||n==0) {

             return 1;

            

      }

            

             return n=n*jiecheng(n-1);

 

}

 

}

 

1.   设计思想

a)  输入第几行第几个;设计组合公式函数调用组合函数;

2.   流程图

3.   代码

 

package one;

 

import javax.swing.JOptionPane;

public class two {

 

      public static void main(String[] args) {

             // TODO 自动生成的方法存根

             String num1,num2;

             int n,k;

             num1=JOptionPane.showInputDialog( "请输入第几行:");

             n=Integer.parseInt( num1 );

             num2=JOptionPane.showInputDialog( "请输入第几个:" );

             k=Integer.parseInt( num2 );

             JOptionPane.showConfirmDialog(null,zuhe(n-1,k),"组合", JOptionPane.CLOSED_OPTION);

 

      }

      public static int C(int n,int k) {

             if(n==1&&k==1)

                    return 1;     

             else if((n==2&&k==1)||(n==2&&k==2))

                    return 1;

             return zuhe(n,k);

      }

      public static int f(int n,int k) {

             int p=1;

             for(int i=1;i<n;i++) {

                   

                    for(int j=1;j<k;j++) {

                           p=C(n-1,k-1)+C(n-1,k);   

                    }

             }

            

             return p;

      }

     

     

      public static int zuhe(int n,int k){

             int p;

             p=jiecheng(n)/(jiecheng(k)*jiecheng(n-k));

            

             return p;

      }

      public static int jiecheng(int n) {

            

             if(n==1||n==0) {

                    return 1;

                   

             }

                   

                    return n=n*jiecheng(n-1);

 

      }

 

}

 

 

1. 设计思想

输入n,k;p=C(n-1,k-1)+C(n-1,k);

2.流程图

3.代码

   import java.util.Scanner;

public class Ncheng2 {

 

      public static void main(String args[]) {

            

             int n,k,tem;

             System.out.print("从n个中选k个,n=");

             Scanner scan=new Scanner(System.in);

             n=scan.nextInt();

             System.out.print("从n个中选k个,k=");

             k=scan.nextInt();

            

             tem=C (n,k);

            

             System.out.println("在"+n+"中选"+k+"个,有多少种方法:"+tem);

            

      }

      public static int C(int n,int k) {

 

             int x;

             if(n==k)

                    return 1;

             else if(k==1)

                    return n;

             else

                x=C(n-1,k)+C(n-1,k-1);

             return x;

      }

}

 

二、

1.设计思想

   输入有几个盘子,如果等于一,输出步骤,如果不等于,减一台词调用;

2. 流程图

 

 

 

 

3. 代码

import java.util.Scanner;

public class HanoiTower {

     

        public static void moveDish(int level, char from, char inter, char to) {

           

            if (level == 1) { 

                System.out.println("从" + from + " 移动盘子" + level + " 号到" + to); 

            }

           

            else {

              

                moveDish(level - 1, from, to, inter); 

                System.out.println("从" + from + " 移动盘子" + level + " 号到" + to); 

                moveDish(level - 1, inter, from, to);

               

            } 

        } 

       

       

        public static void main(String[] args) { 

           

            System.out.println("请选择:");

                    Scanner p=new Scanner(System.in);

                    int nDisks = p.nextInt();

            moveDish(nDisks, \'A\', \'B\', \'C\'); 

           

        } 

    }

三、

1.设计思想

输入字符串区第一个字符为I ,第二个字符为j,如果等于,i++j--;一直到i=j,或者i>j;

2.流程图

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4. 代码

import javax.swing.JOptionPane;

public class three { 

      public static void main(String[] args) {            

             String line=JOptionPane.showInputDialog("请输入字符串:");    

        int i=0;

        int j=line.length()-1;

        JOptionPane.showConfirmDialog(null, "是回文数么?\\n"+three.judge(line,i,j), line, JOptionPane.CLOSED_OPTION);

      }

 

 

public static  boolean judge(String line,int i,int j) {

      if(line.charAt(i)==line.charAt(j))

      {

             if(i==j) {

                    return true;              

             }

            

             else if(i>j) {

                    return true;

             }

             else {

                    i++;       

                    j--;

                    return judge(line,i,j);

             }

      }

      else {

             return false;

            

      }

}

}

 

 

以上是关于课程作业02的主要内容,如果未能解决你的问题,请参考以下文章

课程作业02-1

课程作业02

课程作业02

课程作业02

课程作业02

课程作业02