zoukankan      html  css  js  c++  java
  • JAVA作业 03 方法

    动手动脑

    一.JAVA的类的对象实例化

    1)定义:在面向对象的编程中,通常把用类创建对象的过程称为实例化,其格式为:类名 对象名 = new 类名(参数1,参数2...参数n);

    如 Date date=new Date();就是用日期类创建了一个日期的对象,就叫对象的实例化。实例化一个对象 就是为对象开辟内存空间,或者是不用声明,直接使用new 构造函数名(),建立一个临时对象。
    2)例子:图1:没有对象实例化 

     图2:进行了对象实例化:

    二.利用线性同余法生成随机数

    1)定义:

    2)例子:

    课后作业

    一.组合数问题

    1)程序设计思想:1)利用阶乘方法来实现2)利用杨辉三角的递归来实现3)利用递归来实现

    2)源代码:

    //2016/10/15 XuetongGao

    //组合数的三种实现方式

    import java.io.*;

    public class CombinatorialNumber { 

    public static void main(String[] args) throws IOException{

         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

         String num1 = reader.readLine();  String num2 = reader.readLine();   

         int n = Integer.parseInt(num1);int k = Integer.parseInt(num2);  

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

        {      

                int sum1,sum2,sum3;      

                sum1 = nStratum(n,k);    

                System.out.println("The combinatorial number of (n,k)is " + sum1);      

                sum2 = triangleYH(n,k);

                System.out.println("The combinatorial number of (n+1,k)is " + sum2);

                sum3 = digui(n,k);

                System.out.println("The combinatorial number of (n,k)is " + sum3);   

        }   

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

        {

               System.out.println("The combinatorial number of (n,k)is " + n);  

        }  

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

       {         

               System.out.println("The combinatorial number of (n,k)is 1.");   

       }  

       else

       {          

              System.out.println("Error!please input again!");  

       }

    }

       

    public static int nStratum(int n,int k)//用阶乘来实现组合数问题

    {    

        int n_stratum=1,k_stratum=1,n_k_stratum=1,sum=1;

        int i;   

        for(i=1;i<=n;i++)            {       n_stratum=n_stratum*i;      }     

        for(i=1;i<=k;i++)            {       k_stratum=k_stratum*i;      }    

        for(i=1;i<=(n-k);i++)       {       n_k_stratum=n_k_stratum*i;      }  

        sum = n_stratum/(k_stratum*n_k_stratum);     

        return  sum;

     }  

     public static int digui(int n,int k)//用递归来实现组合数问题  

    {  

         if(k==1)    return n;

         else   

        {    

                    int sum = (digui(n,k-1))*(n-1)/k;    

                    return sum;  

        }  

    }

     public static int triangleYH(int n,int k)//用杨辉三角来实现组合数问题

    {  

           int sum,sum1,sum2;   

           sum1 = digui(n,k);

           sum2 = digui(n,k-1);

           sum = sum1+sum2;

          return sum;     

     }

    }

    3)实验截图

    二.汉诺塔问题

    1)程序设计思想:

    1)在主方法中设置输入流,输入盘子的个数,引入solveTowers方法

    (2)solveTowers方法:若盘子数为1,则不继续递归,结束;若不为1,则将若剩余盘子数不为1 则继续递归 :先将a上上边n-1个盘子移动到b上,将最大的圆盘移动到C上,将B上的圆盘移动到C上;

    (3)在主方法中输出。

    2)源代码:

    //2010/10/14 XuetongGao

    //用递归方式解决汉诺塔问题

     import java.io.*;//引入包

    public class TowersOfHanoi

    {

       public static void main( String[] args ) throws IOException//扔出流

       {

           BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//设定输入流

           String num = reader.readLine();//输入

           int n = Integer.parseInt(num);//强制类型转化

           

           solveTowers(n,'A','B','C');//引用方法

       }

       

       public static void solveTowers(int n,char a,char b,char c)//方法:解决汉诺塔问题

       {

       if (n == 1)//若剩余盘子数为1 则不继续递归 结束

       System.out.println("" + n + " " + a + " 移至 " + c);

           else {//若剩余盘子数不为1 则继续递归 先将a上上边n-1个盘子移动到b

          solveTowers(n - 1, a, c, b);//算法:将上n-1个圆盘移动到B

              System.out.println("" + n + " " + a + " 移至 " + c);//算法:将最大的圆盘移动到C

              solveTowers(n - 1, b, a, c);//算法:B上的圆盘移动到C

           }

       }

    }

    3)实验截图:

    三.运用递归判断某个字符串是否回文

    1)程序设计思想:在主方法中调用判断是否为函数的方法,该函数的参数为(字符串,前对应字符,后对应字符),若两字符的位置不相等,则递归判断一直到两字符的位置相等,同时若两字符相等返回真,若不相等,则返回假。

    2)源代码:

    //2016/10/15 XuetongGao

    //判断字符串是否回文

    public class Palindrome {

        public static boolean isPalindrome(String s,int i,int j)  

       {   if(i>j)    throw new IllegalArgumentException();   

           if(i == j)    return true;   

           else{    return (s.charAt(i)==s.charAt(j))&& isPalindrome(s,i+1,j-1);   }

    }  

        public static void main(String[] args) {

          String test = "ABCBA";

         int i=0;   int j=test.length()-1;

         System.out.println(test + " is Palindrome?" + Palindrome.isPalindrome(test, i, j));

     }  

    }

    3)实验截图:

  • 相关阅读:
    2016.2.17文件夹选择框及文件选择框
    2016.1.22 利用LINQ实现DataSet内多张DataTable关联查询操作(目前未发现太大价值)
    2016.1.23 通过cmd在程序中执行sql脚本
    2016.1.19 DEV Express控件GirdControl使用
    2016.1.1 VS中宏的使用技巧点滴
    2015.12.24(圣诞节) 解决Oralce数据库将具有相同属性的多行合并为一行的简单方法多年想要wmsys.wm_concat
    2015.12.12 DataGridveiw中添加checkbox列
    2015.11.3 RichBox改变若干文本颜色
    2015.12.10 如何将一个工程彻底改名
    2015.9.2 文本框中获取当前位置的所在行和列
  • 原文地址:https://www.cnblogs.com/gxt-/p/5965867.html
Copyright © 2011-2022 走看看