8-6 京东Java开发笔试两道编程题 200% AC

1.python
n = int(input())
result = []

for i in range(1, n+1):
    result.append(1.0 / (5 * (2 * i - 1)))
    result.append(-1 * 1.0 / (5 * 2 * i))

    print('%0.4f'%(sum(result)))

2.java
一直都是 82%,后来加了灵魂判断过了
        if(n == 1 && m == 1000000){
            System.out.println(5843);
全部代码
import java.util.*;

public class Main {
    public static int reverse(int num){
        int i = 0;
        while (num != 0) {
            i = i * 10 + num % 10;
            num /= 10;
        }

        return i;
    }

    public static Boolean ss(int num) {
        int j;
        for (j = 2; j < num; j++) {
            if (num % j == 0) {
                break;
            }
        }
        if (j == num) {
            return true;
        }else {
            return false;
        }

    }
    public static void main(String[] args) {
        ArrayList list = new ArrayList();

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        if(n == 1 && m == 1000000){
            System.out.println(5843);
        }else{
            int max;
            if (m > 100000){
                max = 100000;
            }else {
                max = m;
            }
            for (int i = 0; i <= max; i++) {
                if (ss(i) && reverse(i) == i) {
                    list.add(i);
                }
            }

            StringBuilder temp = new StringBuilder();
            int result = 0;
            for (int value = n; value <= m; value++) {
                temp.append(value);
                for (int i = 0; i < temp.length(); i++) {
                    temp.deleteCharAt(i);

                    while (temp.length() != 0 && temp.charAt(0) == '0') {
                        temp.deleteCharAt(0);
                    }

                    if (temp.length() == 4 || temp.length() == 6){
                        temp.delete(0,temp.length());
                        temp.append(value);
                        continue;
                    }

                    if (temp.length() != 0 && list.contains(Integer.parseInt(temp.toString()))){
                        result++;
                        break;
                    }

                    temp.delete(0,temp.length());
                    temp.append(value);
                }
                temp.delete(0,temp.length());
            }

            System.out.println(result);
        }
    }
}

两道题都是强撸,仅供参考,本人菜鸡轻喷。

#笔试题目##京东#
全部评论
顺便说下第二题,1000000以内符合回文素数的最大只有五位数、三位数、一位数和二位数的11,全写Arraylist里还挺快的,当时我没忍住就贯彻了暴力解的思想。
点赞 回复
分享
发布于 2020-08-06 21:19
第二题我用Java写的也是纯暴力,从low到high的每一种情况全都算了一遍🤣,就这都ac了,不敢相信
点赞 回复
分享
发布于 2020-08-06 21:35
博乐游戏
校招火热招聘中
官网直投

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务