首页 > 8-6 京东Java开发笔试两道编程题 200% AC
头像
花开盛夏~
编辑于 2020-08-07 14:38
+ 关注

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);
        }
    }
}

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

全部评论

(2) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐