JAVA企业面试题精选 Java基础 41-50

1.41.查找有哪几种方法:试写其中一种方法的小例子

参考答案:

  有顺序查找,二分查找,分块查找,二叉排序树查找等。
  下面的sequelSearch方法是顺序查找的案例(顺序查找适合与存储结构为顺序存储或链接存储的线性表)。

public int sequelSearch(String[] s, String key, int n) {
    int i;
    i = 0;
    while(i < n && s[i] != key){
        i++;
        if (s[i] == key) {
            return i;
        }
    }
    return -1;
}

1.42.写代码判断两个数字(x,y)的大小,并返回大数能否被小数整除

参考答案:
public class Q042 {

    public static void main(String[] args) {
        boolean b = judge(10,21);
        System.out.println(b);
    }

    public static boolean judge(int x, int y) {
        return (x < y ? y % x : x % y) == 0;
    }
}

1.43.将一个整型十进制数转化为二进制数(不能使用java的类库)

参考答案:
public class Q043 {

    public static void main(String[] args) {
        String hex = hexConvert(7);
        System.out.println(hex);
    }

    public static String hexConvert(int d) {
        String s = "";
        do {
            int f = d % 2;
            if (f == 1) {
                s = "1" + s;
            } else if (f == 0) {
                s = "0" + s;
            }
            d /= 2;
        } while (d != 0);
        return s;
    }
}

1.44.编一个函数,1000以内,可以被5整除,可以被7整除,但不被5和7同时整除,输出符合结果的所有数

参考答案:
public class Q044 {

    public static void main(String[] args) {
        divide();
    }

    public static void divide() {
        for (int i = 0; i < 1000; i++) {
            if (i % 5 == 0 && i % 7 ==0 || i % 5 != 0 && i % 7 == 0) {
                System.out.println(i);
            }
        }
    }
}

1.45.生成一个六位数的验证码,包括大写字母、小写字母和数字

参考答案:
import java.util.Arrays;

public class Q045 {

    public static void main(String[] args) {
        String[] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 
                'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
                'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9'};
        boolean[] flags = new boolean[letters.length];
        String[] chs = new String[6];
        for (int i = 0; i < chs.length; i++) {
            int index;
            do {
                 index = (int)(Math.random() * (letters.length));
            } while (flags[index]);
            chs[i] = letters[index];
            flags[index] = true;
        }
        String code = Arrays.toString(chs);
        System.out.println(code);
    }   
}

1.46.有这样一类数字,它们顺着看和倒着看是相同的数,例如:121,656,2332等,这样的数字称为回文数字。编写一个java程序,判断从键盘接收的数字是否是回文数字

参考答案:
import java.util.Scanner;

public class Q046 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数字:");
        long inputValueLong = scan.nextLong();
        long temp = inputValueLong;
        long reverseLong = 0L;
        while (inputValueLong != 0) {
            reverseLong = reverserLong * 10 + inputValueLong % 10;
            inputValueLong =inputValueLong / 10;
        }
        if (reverseLong == temp) {
            System.out.println("是回文数");
        } else {
            System.out.println("不是回文数");
        }
    }
}

1.47.编写程序输出9*9乘法口诀

参考答案:
public class Q047 {

    public static void main(Stirng[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + (i * j) + "\t");
            }
            System.out.println("\n");
        }
    }
}

1.48.有五个人坐在一起,问第5个人多少岁?他说比第4个人大2岁。问第4个人多少岁?他说比第3个人大2岁。问第3个人多少岁?他说比第2个人大2岁。问第2个人多少岁?他说比第1个人大2岁。问最后一个人多少岁?他说是10岁。请问第5个人多大?(用递归方式实现)?

参考答案:
public class Q048 {

    public static int getAge(int a) {
        if (a == 1) {
            return 10;
        } else {
            return getAge(a-1) + 2;
        }
    }

    public static void main(String[] args) {
        int a = 5;
        int b = getAge(a);
        System.out.println(b);
    }
}

1.49.公鸡每只3元,母鸡每只5元,小鸡3只1元,问100元买100只鸡有几种买法,请编程?

参考答案:
public class Q049 {

    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i <= 100 * 3; i += 3) { // 买小鸡的只数只能是3的倍数
            for( int j = 0; j <= 100 / 3; j++) { // 买公鸡的只数
                for (int k = 0; k <= 100 /5; k++) { // 买母鸡的只数
                    if (i / 3 + j * 3 + k * 5 == 100 && i + j + k == 100) {
                        count++;
                        System.out.println("小鸡" + i + "只,公鸡" + j + "只,母鸡" + k + "只");
                    }
                }
            }
        }
        System.out.println("共" + count + "种买法");
    }
}

1.50.编程:有1020个西瓜,第一天卖一半多两个,以后每天卖剩下的一半多两个,问几天后能卖完

参考答案:
public class Q050 {

    public static void main(String[] args) {
        int i;
        int num = 1020;
        for (i = 1; ; i++) {
            num = num - (num / 2 + 2); // 当天卖后剩下多少个
            System.out.println("第" + i + "天后剩下" + num + "个");
            if (num == 0) {
                break;
            } 
        }
        System.out.println(i + "天买完");
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值