Java经典练习题_Day05

一、 选择题

1.下列各项中的各项定义正确的是:(ACD)

   A.  public static void m(){}

     B.  public void static m(){}

   C.  public static int m(){return 1.0;}

   D.  public static double m(){return 'a';}

   E.  public static m(){}

2.对于代码:(ACD)

public class Test {

    public static int sum(int i, int j){

        return i + j;
    }
    // 1
}

A. public static int sum(int j, int i){

     return i + j;

    }

B. public static void sum(int i){

      return i * 2;

    }

C. public static int sums(int i, int j){

     return i + j;

    }

D. public static int sum(double i, double j){

     return i + j;

    }

E. public static char sum(char i, char j){

     return i + j;

    }

3.对于代码:(C)

结果为:

  A. 10 2 3 4 5

  B. 1 2 3 4 5

  C. 10 2 3 4 5 0 0 0 0 0

  D. 1 2 3 4 5 0 0 0 0 0

  E. 编译报错

二、 简答题

1、简述方法在定义过程中需要注意的问题

  1.方法不能定义在另一个方法里面
  2.写错方法名字
  3.写错参数列表
  4.方法返回值是void,方法中可以省略return不写
   return 下面不能写代码
  5.方法返回值类型和return后面的数据类型必须匹配
  6.方法不能重复定义
  7.调用方法时,返回值是void,不能写在输出语句中(返回值为空,无法输出)

2、简述方法的重载

  就是方法名相同,方法参数的个数和类型不同,通过个数和类型的不同来区分不同的函数;

  方法的重载跟返回值类型和修饰符无关,Java的重载是发生在本类中的,重载的条件实在本类中有多个方法名相同,

  但参数列表不同(可能是,参数个数不同参数类型不同)跟返回值无关;

3、请对递归与循环进行比较

相同:

  递归与循环都是解决 重复操作的机制

不同:

      就算法效率而言,递归算法的实现往往要比迭代算法消耗更多的时间(调用和返回均需要额外的时间)

      与存储空间(用来保存不同次调用情况下变量的当前值得栈空间)也限制了递归的深度。

      每个迭代算法原则上总可以转换成与它等价的递归算法,反之不然。

      递归的层次是可以控制的,而循嵌套的层次只能是固定的,因此递归是比循环更灵活的重复操作机制。

递归算法解题通常有三个步骤:

  1.分析问题 寻找递归 找出最大规模问题 与最小规模问题的关系 这样通过递归使问题的规模逐渐变小

  2.设置边界、控制递归、找出停止条件 也就是说算法可解的最小规模问题

  3.设计函数、确定参数 和其他算法模块一样设计函数中的操作及相关操作

三、 编程题

1、写一个函数add,接收两个整数作为参数,返回这两个整数的和。

import java.util.Scanner;

public class add1 {
    public static void main(String[] args) {
        System.out.println("请输入两个整数:");
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        
        System.out.println("两个数的和:"+add(a, b));
    }
    
    public static int add(int a, int b) {
        int sum = a + b;
        
        return sum;
    }
}

2、写一个函数接收一个整数,打印这个整数的所有因子。

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        System.out.println("请输入一个整数:");
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        f(a);
    }
    
    public static void f(int s) {
        int a = s;
        for(int i=1; i<=a; i++) {
            if(a % i == 0)
                System.out.println(i);
        }
    }
}

3、写一个函数,接收一个整数n,输出1+2+3+...+n的和。

public class sum {
    public static void main(String[] args) {
        System.out.println(add(3));
    }
    
    public static int add(int n) {
        int sum;
        if(n==1) {
            sum = 1;
            return sum;
        }
        sum = n + add(n - 1);
        return sum;
    }
}

4、写一个函数,接收一个正整数,输出这个正整数是一个几位数。

import java.util.Scanner;

public class test2 {
    public static void main(String[] args) {
        System.out.println("请输入一个整数:");
        Scanner s = new Scanner(System.in);
        int n = s.nextInt(); 
        System.out.println(f(n));
    }
    
    public static int f(int n) {
        
        int count = 1;
        while(true) {
            if(n / 10 != 0) {
                n = n / 10;
                count++;
            }
            else
                break;
        }
        return count;
    }
}

5、写一个函数,接收一个整数,判断这个整数是否是一个质数。

package s1;

import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        System.out.println("请输入一个整数:");
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        f(n);
    }
    
    public static void f(int n) {
        for(int i=2; i<=n; i++) {
            if(n % i != 0) {
                System.out.println("是质数");
                break;
            }
            else {
                System.out.println("不是质数");
                break;
            }
        }
    }
}

6、写一个函数计算两点(x1,x2)之间的距离。

import java.util.Scanner;

public class test4 {
    public static void main(String[] args) {
        System.out.println("请输入两个坐标:");
        Scanner s = new Scanner(System.in);
        double x1 = s.nextInt();
        double x2 = s.nextInt();
        System.out.println(f(x1,x2));
    }
    
    public static double f(double x1, double x2) {
        
        double sum = x2 - x1;
        return sum;
    }
}

7、求abc 和xyz。

已知两个完全平方三位数abc 和xyz,其中a、 b、 c、 x、 y、 z 未必是不同的,而ax、 by、 cz 是三个完全平方数。

看不懂题目

8、求3000以内的全部亲密数。

如果整数A 的全部因子(包括1,不包括A 本身)之和等于B,且整数B 的全部因子包括1,不包括B 本身)之和等于A,则称整数AB 是一对亲密数。

public class AB {
    public static void main(String[] args) {
        for(int j=1; j<=3000; j++) {
            int a = sum(j);
            int b = sum(a);
            if(b == j && j < a) {
                System.out.println(a+" "+b);
            }
        }
    }
    
    public static int sum(int a) {
        int sum = 0;
        for(int i=1; i<=a/2; i++) {
            if(a % i == 0) {
                sum += i;
            }
        }
        return sum;
    }
}

9、验证哥德巴赫猜想

任何一个大于6  的偶数,都能分解成两个质数的和。要求输入一个整数,输出这个数能被分解成哪两个质数的和。

例如: 14

  14=3+11

  14=7+7

package Day8_09;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        System.out.println("请输入一个大于6的偶数:");
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        f(n);
    }
    public static void f(int n){
        
        if(n % 2 == 0 && n > 6){
            for(int i=1; i<n; i++){
                if(i % 2 != 0)       {
                    for(int j=1;j<n;j++){
                        if(j  %  2  !=  0) {
                            if(i  +  j  ==  n){
                                System.out.println(i+" "+j);
                            }
                        }
                    }
                }
            }
        }
    }
}

10、输入一个数字n,利用递归求出这个1~n的和。

import java.util.Scanner;

public class test2 {
    public static void main(String[] args) {
        System.out.println("请输入一个数字:");
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        System.out.println(add(n));
    }
    
    public static int add(int n){
        if(n == 1)
            return 1;
        return n + add(n-1);
    }
}

11、请使用递归获取斐波那契数列的第n项。

斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368……

特别指出:第0项是0,第1项是第一个1。

这个数列从第三项开始,每一项都等于前两项之和。

import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        System.out.print("请输入一个数字:");
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        
        for (int count = 0; count <= n; count++){
            System.out.print(f(count)+" ");
        }
    }
    
    public static int f(int n){
        if(n == 1 || n == 0)
            return n;
        else
            return f(n-1) + f(n-2);
    }
}
原文地址:https://www.cnblogs.com/chuijingjing/p/9445279.html