一亩三分地论坛

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

一亩三分地官方iOS手机应用下载
查看: 643|回复: 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
. 鍥磋鎴戜滑@1point 3 acres

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


[size=100%]import java.util.*;
[size=100%][size=100%]
[size=100%]3


[size=100%]. more info on 1point3acres.com
[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 {
[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
.鐣欏璁哄潧-涓浜-涓夊垎鍦
. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
[size=100%]            pairs = new Pair(sc.nextInt(), sc.nextInt());
[size=100%][size=100%]
[size=100%]13

. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%]        }
[size=100%][size=100%]
[size=100%]14


[size=100%]        int[] res = calculate(pairs, Q);
[size=100%][size=100%]
[size=100%]15
. From 1point 3acres bbs

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


[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
. 鍥磋鎴戜滑@1point 3 acres

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

. visit 1point3acres.com for more.
[size=100%]        
[size=100%][size=100%]
[size=100%]21

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



[size=100%]        Arrays.sort(pairs, new Comparator<Pair>(){
[size=100%][size=100%]
[size=100%]22

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



[size=100%]            public int compare(Pair a, Pair b) {
[size=100%][size=100%]
[size=100%]23
. 1point 3acres 璁哄潧

[size=100%]                return a.val-b.val;
[size=100%][size=100%]
[size=100%]24


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

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


[size=100%]        
[size=100%][size=100%]
[size=100%]27


[size=100%]        int[] indexs = new int[pairs.length];
[size=100%][size=100%]
[size=100%]28


[size=100%]        int sum = 1;
[size=100%][size=100%]
[size=100%]29
. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%][size=100%][size=1.25em]▾



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


[size=100%]            indexs = sum;
[size=100%][size=100%]
[size=100%]31

. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%]            sum += pairs.count;
[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];
. Waral 鍗氬鏈夋洿澶氭枃绔,[size=100%][size=100%]
[size=100%]35
.鏈枃鍘熷垱鑷1point3acres璁哄潧
[size=100%][size=100%][size=1.25em]▾

.鐣欏璁哄潧-涓浜-涓夊垎鍦

[size=100%]        for (int k = 1; k < Q; k++) {. From 1point 3acres bbs
[size=100%][size=100%]
[size=100%]36


[size=100%]            int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));. 1point3acres.com/bbs
[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;.1point3acres缃
[size=100%][size=100%]
[size=100%]39


[size=100%]        }. from: 1point3acres.com/bbs
[size=100%][size=100%]
[size=100%]40

.鐣欏璁哄潧-涓浜-涓夊垎鍦
[size=100%]        return res;
[size=100%][size=100%]
[size=100%]41


[size=100%]    }. 鍥磋鎴戜滑@1point 3 acres
[size=100%][size=100%]
[size=100%]42


[size=100%]   
[size=100%][size=100%]
[size=100%]43

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

.鏈枃鍘熷垱鑷1point3acres璁哄潧
.鐣欏璁哄潧-涓浜-涓夊垎鍦
[size=100%]    static class Pair{
[size=100%][size=100%]
[size=100%]44

. 1point 3acres 璁哄潧
[size=100%]        public int val = 0;
[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]▾


-google 1point3acres
[size=100%]        public Pair(int val, int count) {. 1point 3acres 璁哄潧
[size=100%][size=100%]
[size=100%]47
.1point3acres缃
. Waral 鍗氬鏈夋洿澶氭枃绔,
[size=100%]            this.val = val;
[size=100%][size=100%]
[size=100%]48


[size=100%]            this.count = count;
[size=100%][size=100%]
[size=100%]49
鏉ユ簮涓浜.涓夊垎鍦拌鍧.
. 1point3acres.com/bbs
[size=100%]        }
[size=100%][size=100%]
[size=100%]50


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


[size=100%]}
. from: 1point3acres.com/bbs

 楼主| helloc93 发表于 2016-10-2 16:18:38 | 显示全部楼层
关注一亩三分地公众号:
Warald_一亩三分地
  1. 1
  2. . from: 1point3acres.com/bbs
  3. import java.io.*;
  4. 2
  5. . visit 1point3acres.com for more.
  6. import java.util.*;
  7. 3


  8. . From 1point 3acres bbs
  9. 4
  10. . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  11. . 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴

  12. public class Solution {
  13. 5



  14.     public static void main(String args[] ) throws Exception {
  15. 6

  16.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  17. 7
  18. . more info on 1point3acres.com
  19.         Scanner sc = new Scanner(System.in);. 鍥磋鎴戜滑@1point 3 acres
  20. 8

  21.         int Q = sc.nextInt();
  22. 9

  23.         int M = sc.nextInt();
  24. 10. more info on 1point3acres.com

  25.         Pair[] pairs = new Pair[M];
  26. 11
  27. . From 1point 3acres bbs


  28.         for (int i = 0; i < M; i++) {
  29. 12

  30.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  31. 13
    . 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷

  32.         }
    . 1point 3acres 璁哄潧
  33. 14

  34.         int[] res = calculate(pairs, Q);
  35. 15. 1point 3acres 璁哄潧

  36.         for (int n: res) System.out.println(n);
    -google 1point3acres
  37. 16

  38.     }
  39. 17


  40. 18


  41. . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  42.     public static int[] calculate(Pair[] pairs, int Q) {
  43. 19

  44.         if (pairs.length == 0 || Q < 2) return new int[0];
  45. 20


  46. 21

  47. . more info on 1point3acres.com

  48. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  49.         Arrays.sort(pairs, new Comparator<Pair>(){
  50. 22

  51. . 鍥磋鎴戜滑@1point 3 acres
  52. .鐣欏璁哄潧-涓浜-涓夊垎鍦
  53.             public int compare(Pair a, Pair b) {
  54. 23

  55.                 return a.val-b.val;. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴
  56. 24. From 1point 3acres bbs
  57. . 鍥磋鎴戜滑@1point 3 acres
  58.             }. 1point3acres.com/bbs
  59. 25

  60.         });
  61. 26


  62. 27

  63.         int[] indexs = new int[pairs.length];
  64. 28

  65.         int sum = 1;
  66. 29
  67. . 1point 3acres 璁哄潧


  68.         for (int i = 0; i < indexs.length; i++) {
  69. 30.鐣欏璁哄潧-涓浜-涓夊垎鍦

  70.             indexs[i] = sum;. more info on 1point3acres.com
  71. 31

  72.             sum += pairs[i].count;
  73. 32

  74.         }
  75. 33
  76. .鏈枃鍘熷垱鑷1point3acres璁哄潧
  77. -google 1point3acres
  78. 34

  79.         int[] res = new int[Q-1];
  80. 35. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴


  81. 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  82.         for (int k = 1; k < Q; k++) {
  83. 36

  84.             int index = Arrays.binarySearch(indexs, (int)Math.ceil(((double)pairs.length)*k/Q));
  85. 37. 1point3acres.com/bbs

  86.             if (index < 0) index = -index-1;.1point3acres缃
  87. 38

  88.             res[k-1] = pairs[index].val;
  89. 39
  90. . visit 1point3acres.com for more.
  91.         }
  92. 40.1point3acres缃

  93.         return res;
  94. 41

  95.     }. 鍥磋鎴戜滑@1point 3 acres
  96. 42


  97. 43. 鐣欏鐢宠璁哄潧-涓浜╀笁鍒嗗湴



  98.     static class Pair{
  99. 44

  100.         public int val = 0;
  101. 45
  102. . visit 1point3acres.com for more.
  103.         public int count = 0;
  104. 46



  105.         public Pair(int val, int count) {.1point3acres缃
  106. 47

  107.             this.val = val;
  108. 48
    -google 1point3acres

  109.             this.count = count; 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
  110. 49

  111.         }. From 1point 3acres bbs
  112. 50

  113.     }
  114. 51. visit 1point3acres.com for more.

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

使用道具 举报

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

使用道具 举报

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

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

  2. import java.io.*;.1point3acres缃
  3. 2

  4. import java.util.*;
  5. 3



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



  7. public class Solution {
  8. 5



  9.     public static void main(String args[] ) throws Exception {
  10. 6. from: 1point3acres.com/bbs

  11.         /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  12. 7

  13.         Scanner sc = new Scanner(System.in);
  14. 8

  15.         int Q = sc.nextInt();
  16. 9

  17.         int M = sc.nextInt();
  18. 10 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  19.         Pair[] pairs = new Pair[M];
  20. 11



  21.         for (int i = 0; i < M; i++) {.1point3acres缃
  22. 12

  23.             pairs[i] = new Pair(sc.nextInt(), sc.nextInt());
  24. 13
  25. . more info on 1point3acres.com
  26.         }
  27. 14

  28.         int[] res = calculate(pairs, Q);
  29. 15

  30.         for (int n: res) System.out.println(n);
  31. 16

  32.     }.鐣欏璁哄潧-涓浜-涓夊垎鍦
  33. 17


  34. 18. From 1point 3acres bbs



  35.     public static int[] calculate(Pair[] pairs, int Q) {
  36. 19

  37.         if (pairs.length == 0 || Q < 2) return new int[0];.1point3acres缃
  38. 20
  39. . From 1point 3acres bbs

  40. 21



  41.         Arrays.sort(pairs, new Comparator<Pair>(){
  42. 22



  43.             public int compare(Pair a, Pair b) {
  44. 23

  45.                 return a.val-b.val;. visit 1point3acres.com for more.
  46. 24
  47. -google 1point3acres
  48.             }
  49. 25
  50. -google 1point3acres
  51.         });
  52. 26. Waral 鍗氬鏈夋洿澶氭枃绔,


  53. 27-google 1point3acres

  54.         int[] indexs = new int[pairs.length];
  55. 28 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  56.         int sum = 1;
  57. 29



  58.         for (int i = 0; i < indexs.length; i++) {
  59. 30

  60.             indexs[i] = sum;
  61. 31 鏉ユ簮涓浜.涓夊垎鍦拌鍧.

  62.             sum += pairs[i].count;
  63. 32

  64.         }
  65. 33


  66. 34.鏈枃鍘熷垱鑷1point3acres璁哄潧

  67.         int[] res = new int[Q-1];.鐣欏璁哄潧-涓浜-涓夊垎鍦
  68. 35. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷



  69.         for (int k = 1; k < Q; k++) {
  70. 36

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

  73.             if (index < 0) index = -index-1;
  74. 38.鐣欏璁哄潧-涓浜-涓夊垎鍦

  75.             res[k-1] = pairs[index].val;. From 1point 3acres bbs
  76. 39

  77.         }. Waral 鍗氬鏈夋洿澶氭枃绔,
  78. 40
  79. . 1point 3acres 璁哄潧
  80.         return res;
  81. 41

  82.     }
  83. 42


  84. 43
  85. . Waral 鍗氬鏈夋洿澶氭枃绔,


  86. . 1point3acres.com/bbs
  87.     static class Pair{. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
  88. 44

  89.         public int val = 0;
  90. 45

  91.         public int count = 0;. more info on 1point3acres.com
  92. 46



  93.         public Pair(int val, int count) {
  94. 47

  95.             this.val = val;
  96. 48

  97.             this.count = count;
  98. 49

  99.         }
  100. 50

  101.     }
  102. 51

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

使用道具 举报

本版积分规则

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

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

关闭

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

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

custom counter

GMT+8, 2017-4-23 20:53

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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