zoukankan      html  css  js  c++  java
  • java方法课后作业

    一.请看以下代码,你发现了有什么特殊之处吗?


    特殊之处:方法名一样,但是输出结果不一样!
    方法名相同区分的方式(用square方法为例)
    ·参数类型不同,square(int x),square(double y)
    ·参数个数,square(int x,int y)square(int x)
    ·参数类型顺序不同,square(int x,double y)square(double x,int y)
    二.在SquareIntTest中,把Square 方法中的static删了会有什么情况
    ·程序无法运行,并报错
    ·在main方法中,用类SquareIntTest定义一个新的类名字即SquareIntTest s=new SquareIntTest();这样,在调用方法既可以类名.方法名来调用方法,即s.square(x);
    三.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

    四.课后作业1使用计算机计算组合数
    ·使用组合数公式利用n!来计算,

            import java.io.*;
    public class Jiechen {
     public static void main(String[] args)throws IOException{
      System.out.println("输入n的值:" );
      double n,k;
      BufferedReader reader = new BufferedReader(new  InputStreamReader(System.in));
      String readn=reader.readLine();
      n=Double.parseDouble(readn);
      System.out.println("输入k的值:" );
      String readk=reader.readLine();
      k=Double.parseDouble(readk);
      double result=che(n)/(che(k)*che(n-k));
      System.out.println(result);
     }
     public static double che(double n)
     {
      double result=1;
      if(n>0)
       result=che(n-1)*n;
      else if(n==0||n==1)
       result=1;
      else
       System.out.println("输入非法");
      return result;
     }
    }

    ·使用递推的方法用杨辉三角计算


    import java.io.*;
    public class yanghui {
     public static void main(String[] args)throws IOException {
      double n,k;
      System.out.print("请输n:");
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      String readn=reader.readLine();
      n=Double.parseDouble(readn);
      
      System.out.print("请输入k:");
      String readk=reader.readLine();
      k=Double.parseDouble(readk);

      double r= C(n,k);
      System.out.println(" 公式计算结果为: "+r);
     }
       public static int C(double n,double k)
       {
        int result=0;
        if(n==1||k==0||n==k)
         result= 1;
        else
         result= C(n-1,k-1)+C(n-1,k);
        return result;
       }
    }

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

    五.课后作业2递归编程解决汉诺塔问题。用Java实现

    import java.io.*;
    public class TowersOfHanoi{
     public static void main( String[] args ) throws IOException{
      System.out.println("输入盘子的个数:");
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      String readnum = reader.readLine();
      int n = Integer.parseInt(readnum);
      solveTowers(n,'A','B','C');
       }

       public static void solveTowers(int n,char a,char b,char c){
        if (n == 1)
         System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
        else {
         solveTowers(n - 1, a, c, b);
         System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
         solveTowers(n - 1, b, a, c);
           }
       }
    }


    六.课后作业3使用递归方式判断某个字串是否是回文( palindrome )
    import java.io.*;
    public class Huiwen {
     public static void main(String[] args)throws IOException {
     System.out.println("请输入一个字符串:");
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     String str=reader.readLine();
     if(huijudge(str)==1)
     System.out.println(str+"是回文!");
     else
     System.out.println(str+"不是回文!");
     }
     public static int huijudge(String str){
      int judge=1;
      int length=str.length();
      //charAT()是把字符串拆分获取其中的某个字符,返回指定位置的字符。 
      char f=str.charAt(0);
      char l=str.charAt(length-1);
      if(length==0||length==1)
       return judge=1;
      if(f!=l)
       return judge=0;
      //substring() 方法用于提取字符串中介于两个指定下标之间的字符。
      return huijudge(str.substring(1,length-1));
     }
    }

  • 相关阅读:
    IOS之UIKit_Day13
    IOS之UIKit_Day12
    IOS之UIKit_Day11
    IOS之UIKit_Day10
    iOS设计模式之工厂模式
    常用操作
    盒子模式
    block循环使用问题
    IOS-sqlite3
    IOS-View用作控制器
  • 原文地址:https://www.cnblogs.com/wf1647790534/p/5966023.html
Copyright © 2011-2022 走看看