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

    动手动脑:

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

    // MethodOverload.java

    // Using overloaded methods

    public class MethodOverload {

             public static void main(String[] args) {

                       System.out.println("The square of integer 7 is " + square(7));

                       System.out.println(" The square of double 7.5 is " + square(7.5));  

       }

             public static int square(int x) {

                       return x * x;

             }

             public static double square(double y) {

                       return y * y;

             }

    }

    答:

    在一个类中有两个同名的方法,上述示例代码展示了Java的“方法重载(overload)”特性。

    满足以下条件的两个或多个方法构成“重载”关系:

    (1) 方法名相同;

    (2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。

    注意:方法的返回值不作为方法重载的判断条件。

    原因,方法地返回值在最后出结果地时候才会进行判断是否符合条件固不能用返回值作为方法重载判断的标准。

    练习: 查看一下JDK中System.out.println()方法,你发现了什么?

    ”+“在java里可以进行字符串拼接!
    System.out.println(i);//这样是直接输出变量
    System.out.println("11111");//这是直接输出
    System.out.println("i = "  + i);//这是拼接的,如果i=1输出“i=1”,如果i=“string”输出“i=string”。
    System.out.println(i+i+“222a”+j+j);
    当多个变量或变量和字符串相加时,变量和字符串一般就是互相拼接。
    但当某些变量为int类型时,如上面的int i=1 j=1;输出“2222a11”。
    可以看出int类型在前相加时是先进行加法运算的,后面则是默认转换成String类型。

    Java中输出语句是重载过地可以识别多种不同的连接方式

     2 .编写一个方法,使用纯随机数发生器生成指定数目(比如1000个)的随机整数。

    import java.math.*;

    public class test2 {

      public static void main(String[] args) {

        // TODO Auto-generated method stub

          int x=57,a=95,c=17, m=13;int k=0;//种子值

        for(int i=0;i<1000;i++){

          x=(a*x+c)%m;//随机数生出算法

          a+=5;x+=2;c++;m+=4;

          k++;

          System.out.print(x+" ");

          if(k%5==0)

             System.out.println();

          }

      }

    }

     

    课后作业:

    使用计算机计算组合数:

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

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

    程序代码: 

    public class YangHuitriangle {

       public static void main(String[] args) {

        // TODO Auto-generated method stub

        System.out.println(CombinationNumber(5,2));

      }

      public static long Factorial(long n){

        long sum=1;

          if(n<0)

           System.out.println("输入的值非法!!!");

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

           sum=1;

           else sum=Factorial(n-1)*n;

          return sum;

           }

        public static long CombinationNumber(long m,long n){

        long num;

        if(n==0||n==m)

          num=1;

        else

        num=CombinationNumber(m-1,n-1)+CombinationNumber(m-1,n);//递归做法

        //num=Factorial(m)/(Factorial(n)*Factorial(m-n));(阶乘做法)

        return num;

      }

    }

     编程思路:先明确大的问题如阶乘的算法然后找到他与下一级的联系然后就可以编写递归算法了,阶乘算法无非是调用阶乘,递归算法是利用了组合数的规律每个数都等于他头顶两数的和如果没有数字及为1这边是终止条件。

    截图:

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

    代码:

    public class HanoiProblem {

       public static void main(String[] args) {

        // TODO Auto-generated method stub

        System.out.println("移动一个40层的汉诺塔,需要移动  "+Hanoiproblem(40)+"次");

      }

      //递归调用自身的n-1计算n。

        public static long Hanoiproblem(long n){

        long num=0;

        if(n==1)

           num=1;

        else num=2*Hanoiproblem(n-1)+1;

        return num;

        }

    }

    实验思路:找到n和n-1之间的关系,找到结束的条件即可

    截图:

    课后作业3

    使用递归方式判断某个字串是否是回文( palindrome )

    “回文”是指正着读、反着读都一样的句子。比如“我是谁是我”

    使用递归算法检测回文的算法描述如下:

    A single or zero-character string is a palindrome.

    Any other string is a palindrome if the first and last characters are the same, and the string that remains, excepting those characters, is a palindrome.

    代码为:

     public class PalindromeString {

      public static void main(String[] args) {

          String text = "你是谁是你";

          System.out.println(isHuiwen(text));

         }

         public static boolean isHuiwen(String text){

          int length = text.length();

          for(int i=0;i<length/2;i++){

           if(text.charAt(i)!=text.charAt(length-i-1)){

            return false;

           }

          }

          return true;

         }

    }

    实验思路:将字符串读入string字符串然后再得到字符串的长度进行比较

    截图:

  • 相关阅读:
    SpringBoot入门
    Maven相关
    IO流之RandomAccessFile和File
    javaSE之运行时异常和编译时异常
    Map.Entry的由来和使用
    Oracle常用基础语法(未完待补和操作)
    Oracle存储过程和存储函数
    论文阅读笔记(三十七)【AAAI2020】:Frame-Guided Region-Aligned Representation for Video Person Re-identification
    论文阅读笔记(三十六)【AAAI2020】:Relation-Guided Spatial Attention and Temporal Refinement for Video-based Person Re-Identification
    论文阅读笔记(三十五)【CVPR2019】:Perceive Where to Focus: Learning Visibility-aware Part-level Features for Partial Person Re-identification
  • 原文地址:https://www.cnblogs.com/wangfengbin/p/5965315.html
Copyright © 2011-2022 走看看