一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 380|回复: 3
收起左侧

Indeed OA 15

[复制链接] |试试Instant~ |关注本帖
helloc93 发表于 2016-10-2 16:17:47 | 显示全部楼层 |阅读模式

2016(7-9月) 码农类 硕士 全职@Indeed - 内推 - 在线笔试 |Otherfresh grad应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
刚做的Indeed OA 15,挂了两个test case 4跟6,估计是挂了。附上写的代码,求大神帮忙debug。hackerrank不能看到test case是什么,真的很坑 085435d3g5fnijc5taicbr.png
[size=100%][size=100%]
[size=100%]1
. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

[size=100%]import java.io.*;
[size=100%][size=100%]
[size=100%]2

. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
[size=100%]import java.util.*;
[size=100%][size=100%]
[size=100%]3

. more info on 1point3acres.com
[size=100%]
[size=100%][size=100%]
[size=100%]4

[size=100%][size=100%][size=1.25em]▾

. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
.鐣欏璁哄潧-涓浜-涓夊垎鍦
[size=100%]public class Solution {. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
[size=100%][size=100%]
[size=100%]5

[size=100%][size=100%][size=1.25em]▾



[size=100%]    public static void main(String args[] ) throws Exception {-google 1point3acres
[size=100%][size=100%]
[size=100%]6


[size=100%]        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
[size=100%][size=100%]
[size=100%]7


[size=100%]        Scanner sc = new Scanner(System.in);
[size=100%][size=100%]
[size=100%]8


[size=100%]        int Q = sc.nextInt();
[size=100%][size=100%]
[size=100%]9


[size=100%]        int M = sc.nextInt();. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
[size=100%][size=100%]
[size=100%]10


[size=100%]        Pair[] pairs = new Pair[M];
[size=100%][size=100%]
[size=100%]11

[size=100%][size=100%][size=1.25em]▾



[size=100%]        for (int i = 0; i < M; i++) {
[size=100%][size=100%]
[size=100%]12

. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%]            pairs = new Pair(sc.nextInt(), sc.nextInt());
[size=100%][size=100%]
[size=100%]13


[size=100%]        }
[size=100%][size=100%]
[size=100%]14


[size=100%]        int[] res = calculate(pairs, Q);. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
[size=100%][size=100%]
[size=100%]15


[size=100%]        for (int n: res) System.out.println(n);
[size=100%][size=100%]
[size=100%]16

. 1point 3acres 璁哄潧
[size=100%]    }
[size=100%][size=100%]
[size=100%]17


[size=100%]   
[size=100%][size=100%]
[size=100%]18

[size=100%][size=100%][size=1.25em]▾
.鐣欏璁哄潧-涓浜-涓夊垎鍦

. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
[size=100%]    public static int[] calculate(Pair[] pairs, int Q) {
[size=100%][size=100%]
[size=100%]19


[size=100%]        if (pairs.length == 0 || Q < 2) return new int[0];
[size=100%][size=100%]
[size=100%]20


[size=100%]        
[size=100%][size=100%]
[size=100%]21

[size=100%][size=100%][size=1.25em]▾


. From 1point 3acres bbs
[size=100%]        Arrays.sort(pairs, new Comparator<Pair>(){
[size=100%][size=100%]
[size=100%]22

[size=100%][size=100%][size=1.25em]▾

. 鍥磋鎴戜滑@1point 3 acres

[size=100%]            public int compare(Pair a, Pair b) {
[size=100%][size=100%]
[size=100%]23

. From 1point 3acres bbs
[size=100%]                return a.val-b.val;
[size=100%][size=100%]
[size=100%]24


[size=100%]            }
[size=100%]
[size=100%]
[size=100%]25


[size=100%]        });
[size=100%][size=100%]
[size=100%]26


[size=100%]         鏉ユ簮涓浜.涓夊垎鍦拌鍧.
[size=100%][size=100%]
[size=100%]27

. visit 1point3acres.com for more.
[size=100%]        int[] indexs = new int[pairs.length];
[size=100%][size=100%]
[size=100%]28

-google 1point3acres
[size=100%]        int sum = 1;
[size=100%][size=100%]
[size=100%]29

[size=100%][size=100%][size=1.25em]▾
. 鍥磋鎴戜滑@1point 3 acres


[size=100%]        for (int i = 0; i < indexs.length; i++) {
[size=100%][size=100%]
[size=100%]30


[size=100%]            indexs = sum;. from: 1point3acres.com/bbs
[size=100%][size=100%]
[size=100%]31


[size=100%]            sum += pairs.count;.1point3acres缃
[size=100%][size=100%]
[size=100%]32


[size=100%]        }
[size=100%][size=100%]
[size=100%]33
鏉ユ簮涓浜.涓夊垎鍦拌鍧.
. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
[size=100%]        
[size=100%][size=100%]
[size=100%]34


[size=100%]        int[] res = new int[Q-1];
[size=100%][size=100%]
[size=100%]35

[size=100%][size=100%][size=1.25em]▾


. 鍥磋鎴戜滑@1point 3 acres
[size=100%]        for (int k = 1; k < Q; k++) {. more info on 1point3acres.com
[size=100%][size=100%]
[size=100%]36


[size=100%]            int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
[size=100%][size=100%]
[size=100%]37


[size=100%]            if (index < 0) index = -index-1;
[size=100%][size=100%]
[size=100%]38

. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

[size=100%]            res[k-1] = pairs[index].val;
[size=100%][size=100%]
[size=100%]39


[size=100%]        }
[size=100%][size=100%]
[size=100%]40


[size=100%]        return res;. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
[size=100%][size=100%]
[size=100%]41


[size=100%]    }
[size=100%][size=100%]
[size=100%]42


[size=100%]    .鏈枃鍘熷垱鑷1point3acres璁哄潧
[size=100%][size=100%]
[size=100%]43

[size=100%][size=100%][size=1.25em]▾



[size=100%]    static class Pair{
[size=100%][size=100%]
[size=100%]44
. 鍥磋鎴戜滑@1point 3 acres

[size=100%]        public int val = 0;. from: 1point3acres.com/bbs
[size=100%][size=100%]
[size=100%]45


[size=100%]        public int count = 0;
[size=100%][size=100%]
[size=100%]46

[size=100%][size=100%][size=1.25em]▾



[size=100%]        public Pair(int val, int count) {
[size=100%][size=100%]
[size=100%]47


[size=100%]            this.val = val;
[size=100%][size=100%]
[size=100%]48

. 1point3acres.com/bbs
[size=100%]            this.count = count;
[size=100%][size=100%]
[size=100%]49
. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

[size=100%]        }
[size=100%][size=100%]
[size=100%]50


[size=100%]    }
[size=100%][size=100%]
[size=100%]51


[size=100%]}


. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
 楼主| helloc93 发表于 2016-10-2 16:18:38 | 显示全部楼层
  1. 1

  2. import java.io.*;
  3. 2. 1point 3acres 璁哄潧
  4. . 1point 3acres 璁哄潧
  5. import java.util.*;. From 1point 3acres bbs
  6. 3

  7. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  8. 4



  9. public class Solution {.1point3acres缃
  10. 5


  11. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  12.     public static void main(String args[] ) throws Exception {
  13. 6
  14. . more info on 1point3acres.com
  15.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */. 鍥磋鎴戜滑@1point 3 acres
  16. 7
  17. -google 1point3acres
  18.         Scanner sc = new Scanner(System.in);. 鍥磋鎴戜滑@1point 3 acres
  19. 8

  20.         int Q = sc.nextInt();
  21. 9. 鍥磋鎴戜滑@1point 3 acres

  22.         int M = sc.nextInt();
  23. 10

  24.         Pair[] pairs = new Pair[M];. more info on 1point3acres.com
  25. 11



  26.         for (int i = 0; i < M; i++) {
  27. 12.鏈枃鍘熷垱鑷1point3acres璁哄潧

  28.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  29. 13

  30.         }
  31. 14

  32.         int[] res = calculate(pairs, Q);
  33. 15
  34. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  35.         for (int n: res) System.out.println(n);. visit 1point3acres.com for more.
  36. 16
  37. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  38.     }
  39. 17. 鍥磋鎴戜滑@1point 3 acres
  40. -google 1point3acres

  41. 18



  42.     public static int[] calculate(Pair[] pairs, int Q) {
  43. 19

  44.         if (pairs.length == 0 || Q < 2) return new int[0];. From 1point 3acres bbs
  45. 20


  46. 21


  47. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  48.         Arrays.sort(pairs, new Comparator<Pair>(){. more info on 1point3acres.com
  49. 22



  50.             public int compare(Pair a, Pair b) {
  51. 23

  52.                 return a.val-b.val;
  53. 24

  54.             }. From 1point 3acres bbs
  55. 25. 鍥磋鎴戜滑@1point 3 acres
  56. -google 1point3acres
  57.         });
  58. 26


  59. 27
  60. . 1point3acres.com/bbs
  61.         int[] indexs = new int[pairs.length];
    鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  62. 28

  63.         int sum = 1;. from: 1point3acres.com/bbs
  64. 29 鏉ユ簮涓浜.涓夊垎鍦拌鍧.


  65. . 1point 3acres 璁哄潧
  66.         for (int i = 0; i < indexs.length; i++) {
  67. 30

  68.             indexs[i] = sum;
  69. 31

  70.             sum += pairs[i].count;.鏈枃鍘熷垱鑷1point3acres璁哄潧
  71. 32
  72. . from: 1point3acres.com/bbs
  73.         }.1point3acres缃
  74. 33


  75. 34

  76.         int[] res = new int[Q-1];
    -google 1point3acres
  77. 35.1point3acres缃


  78. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  79.         for (int k = 1; k < Q; k++) {
  80. 36. From 1point 3acres bbs

  81.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
  82. 37

  83.             if (index < 0) index = -index-1;
  84. 38

  85.             res[k-1] = pairs[index].val;
  86. 39

  87.         }
    . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  88. 40. 1point 3acres 璁哄潧

  89.         return res;
  90. 41

  91.     }. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  92. 42.1point3acres缃


  93. 43

  94. . Waral 鍗氬鏈夋洿澶氭枃绔,

  95.     static class Pair{
  96. 44

  97.         public int val = 0;
  98. 45

  99.         public int count = 0;.1point3acres缃
  100. 46

  101. . 鍥磋鎴戜滑@1point 3 acres

  102.         public Pair(int val, int count) {
  103. 47

  104.             this.val = val;
  105. 48. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  106. -google 1point3acres
  107.             this.count = count;
  108. 49

  109.         }
  110. 50

  111.     }
  112. 51 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  113. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| helloc93 发表于 2016-10-2 16:19:16 | 显示全部楼层
第一次在帖子了插入代码,不会搞。。
回复 支持 反对

使用道具 举报

 楼主| helloc93 发表于 2016-10-2 16:19:51 | 显示全部楼层
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
  1. 1

  2. import java.io.*;
  3. 2

  4. import java.util.*;. From 1point 3acres bbs
  5. 3



  6. 4



  7. public class Solution {
  8. 5

  9. . Waral 鍗氬鏈夋洿澶氭枃绔,
  10. . more info on 1point3acres.com
  11.     public static void main(String args[] ) throws Exception {
  12. 6

  13.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */. Waral 鍗氬鏈夋洿澶氭枃绔,
  14. 7

  15.         Scanner sc = new Scanner(System.in);.鏈枃鍘熷垱鑷1point3acres璁哄潧
  16. 8

  17.         int Q = sc.nextInt();
  18. 9
  19. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  20.         int M = sc.nextInt();. 1point 3acres 璁哄潧
  21. 10

  22.         Pair[] pairs = new Pair[M];
  23. 11

  24. -google 1point3acres

  25.         for (int i = 0; i < M; i++) {.鐣欏璁哄潧-涓浜-涓夊垎鍦
  26. 12

  27.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  28. 13

  29.         }
  30. 14
  31. . From 1point 3acres bbs
  32.         int[] res = calculate(pairs, Q);
  33. 15

  34.         for (int n: res) System.out.println(n);
  35. 16

  36.     }
  37. 17


  38. 18



  39.     public static int[] calculate(Pair[] pairs, int Q) {
  40. 19-google 1point3acres

  41.         if (pairs.length == 0 || Q < 2) return new int[0];
  42. 20 鏉ユ簮涓浜.涓夊垎鍦拌鍧.


  43. 21

  44. . 鍥磋鎴戜滑@1point 3 acres
  45. . Waral 鍗氬鏈夋洿澶氭枃绔,
  46.         Arrays.sort(pairs, new Comparator<Pair>(){ 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  47. 22
  48. . Waral 鍗氬鏈夋洿澶氭枃绔,


  49.             public int compare(Pair a, Pair b) {
  50. 23
  51. . 1point 3acres 璁哄潧
  52.                 return a.val-b.val;
  53. 24

  54.             }
  55. 25
  56. -google 1point3acres
  57.         });. From 1point 3acres bbs
  58. 26


  59. 27

  60.         int[] indexs = new int[pairs.length];
  61. 28

  62.         int sum = 1;
  63. 29



  64.         for (int i = 0; i < indexs.length; i++) {
  65. 30. 1point3acres.com/bbs

  66.             indexs[i] = sum;. From 1point 3acres bbs
  67. 31

  68.             sum += pairs[i].count;
  69. 32

  70.         }
  71. 33


  72. 34
  73. . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
  74.         int[] res = new int[Q-1];
  75. 35
  76. -google 1point3acres


  77.         for (int k = 1; k < Q; k++) {
  78. 36

  79.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
  80. 37.鏈枃鍘熷垱鑷1point3acres璁哄潧

  81.             if (index < 0) index = -index-1;
  82. 38

  83.             res[k-1] = pairs[index].val;. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  84. 39

  85.         }
  86. 40
  87. .1point3acres缃
  88.         return res;
  89. 41

  90.     }
  91. 42. 1point3acres.com/bbs


  92. 43



  93.     static class Pair{. 1point 3acres 璁哄潧
  94. 44

  95.         public int val = 0;
    . from: 1point3acres.com/bbs
  96. 45

  97.         public int count = 0;
  98. 46
  99. . Waral 鍗氬鏈夋洿澶氭枃绔,
  100. . 1point 3acres 璁哄潧

  101.         public Pair(int val, int count) {
  102. 47

  103.             this.val = val;
  104. 48

  105.             this.count = count;
  106. 49

  107.         }
  108. 50

  109.     }
  110. 51
  111. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  112. }
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2017-1-22 18:56

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表