java代码作业_JAVA作业 03 方法

动手动脑

一.JAVA的类的对象实例化

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

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

2)例子:图1:没有对象实例化

ce5104f35ea63fda8c517c2695f43221.png

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

e7df4f808be8fba33049ab98e50b2951.png

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

1)定义:

59be6ec15f21a74fa0c5923093825494.png

2)例子:

4171f3778ec0c7f910ff724e90f03787.png

课后作业

一.组合数问题

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)实验截图

c92ba3cd6bac5c82bbe59cff3f612ef7.png

二.汉诺塔问题

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)实验截图:

673ca6ee941c1d4ff4b9abc20f38697d.png

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

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)实验截图:

bf3436fca63699328c6fedbc55a50f74.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值