京东笔试股票27%为啥呢,一直调不过去

最开始版本:
import java.util.Arrays;
import java.util.Scanner;

public class JingDong {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input[] = scanner.nextLine().split(" ");
        int m = Integer.parseInt(input[0]);
        int n = Integer.parseInt(input[1]);
        int a[] = new int[m];
        String s[] = scanner.nextLine().split(" ");
        for(int i=0;i<m;i++){
            a[i] = Integer.parseInt(s[i]);
        }

        Arrays.sort(a);

        int q = Integer.parseInt(scanner.nextLine());
        for(int j=0;j<q;j++){
            int cur = Integer.parseInt(scanner.nextLine());
            int sum = 0;
            int index = 1;
            int jishu = 0;
            for(int p =cur-1;p>=0;p--){
                jishu++;
                sum = sum + a[p]*index;
                if(jishu==n){
                    index++;
                    jishu=0;
                }
            }
            System.out.println(sum);
        }

    }
}
做了改进后:
import java.util.Arrays;
import java.util.Scanner;

public class JingDong {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input[] = scanner.nextLine().split(" ");
        int m = Integer.parseInt(input[0]);
        int n = Integer.parseInt(input[1]);
        int a[] = new int[m];
        String s[] = scanner.nextLine().split(" ");
        int he[] = new int[m];
        Arrays.fill(he,0);
        for(int i=0;i<m;i++){
            a[i] = Integer.parseInt(s[i]);
            if(i==0) he[i] = a[i];
            else he[i] = he[i-1]+a[i];
        }

        Arrays.sort(a);

        int q = Integer.parseInt(scanner.nextLine());
        for(int j=0;j<q;j++){
            int cur = Integer.parseInt(scanner.nextLine());
            int sum = 0;
            int index = 1;
            for(int p =cur-1;p>=0;p=p-n){
                int c = he[p]-(p>=n?he[p-n]:0);
                sum = sum+c*index;
                index++;

            }
            System.out.println(sum);
        }

    }
}


#京东笔试##笔试题目##京东#
全部评论
python18% 一直在报时间超限😂
1 回复
分享
发布于 2020-04-18 21:05
我也是。。。一开始被牛客的输入输出坑,一直0,,后面调好了一直27,第二题都没做。。。
1 回复
分享
发布于 2020-04-18 21:08
阅文集团
校招火热招聘中
官网直投
和楼主方法一样c++写的,提示运行超时,27% 。坑爹啊
1 回复
分享
发布于 2020-04-18 21:09
我怎么改都是27,最后一题都没写
1 回复
分享
发布于 2020-04-18 21:09
c++一直18
1 回复
分享
发布于 2020-04-18 21:10
我也是27,它报错说时间超限,但是提交记录显示我是1000ms以内,很迷
点赞 回复
分享
发布于 2020-04-18 21:02
一样。。。
点赞 回复
分享
发布于 2020-04-18 21:03
我也是27呢
点赞 回复
分享
发布于 2020-04-18 21:04
同27  +1
点赞 回复
分享
发布于 2020-04-18 21:04
同27
点赞 回复
分享
发布于 2020-04-18 21:04
我python一直18,吐了
点赞 回复
分享
发布于 2020-04-18 21:04
我也一直27,我吐了
点赞 回复
分享
发布于 2020-04-18 21:04
同27,弄了好几遍,也没改善
点赞 回复
分享
发布于 2020-04-18 21:04
同问,这都是什么鬼题
点赞 回复
分享
发布于 2020-04-18 21:05
一样。。都是27.。
点赞 回复
分享
发布于 2020-04-18 21:05
我也是27 感觉复杂度并不大啊
点赞 回复
分享
发布于 2020-04-18 21:05
哇第一题做的我心态崩溃了
点赞 回复
分享
发布于 2020-04-18 21:05
我18% 有点想不通。 #!/usr/bin/env python #coding:utf-8 stocks = list() query_array = list() stock_tran_day = list() try:     inp = input()     stock_in = int(inp.split(' ')[0])     stock_tran_day = int(inp.split(' ')[1])     stocks = [int(s) for s in input().split(' ')]     query_count = int(input())     while query_count > 0:         query_array.append(int(input()))         query_count -= 1 except Exception as e:     print(e)     exit(1) stocks = sorted(stocks) def get_min(counts):     """     :param counts:     :return:     """     min_price = 0     while counts > 0:         for i in range(counts):             min_price += stocks[i]         counts -= stock_tran_day     print(min_price) if __name__ == '__main__':     for day in query_array:         get_min(day)
点赞 回复
分享
发布于 2020-04-18 21:05
我也是27,吐了。觉得时间复杂度也还行吧。
点赞 回复
分享
发布于 2020-04-18 21:05
你们第二题都AC了吗
点赞 回复
分享
发布于 2020-04-18 21:05

相关推荐

头像
04-02 20:00
点赞 评论 收藏
转发
3 5 评论
分享
牛客网
牛客企业服务