返回信息流http://www.nowcoder.com/questionTerminal/acb888f7ccee4fc0aab208393d41a552
这个题有个测试用例是这样的:
测试用例:
281 64937 41837 95518 4598 58724 6668 34466 38934 20702 55490 62431 16323 42596 72230 93272 76770 80006 48823 65846 39258 17746 4758 91048 90281 45370 83317 42210 90968 47770 13135 12558 69449 35195 42351 85668 21023 18949 42852 49959 46691 84167 90269 31403 47204 45070 13055 41575 24319 45582 17990 94505 11562 9673 86077 12350 16794 20373 28496 31890 4153 90018 57981 33800 54734 27315 36354 57986 70409 65900 71929 70812 85299 94692 13132 32571 86462 43907 56841 7381 28013 79366 39679 93376 84534 55392 25776 67722 70789 63734 91037 47681 72483 47349 42031 37750 46402 58296 93695 84998 51820 62096 9493 16563 81675 93837 36007 64706 89800 37454 80575 7264 90596 31612 47230 78525 79846 4464 38316 70432 43834 45505 78314 66577 35042 8153 51208 89018 64035 13861 14885 46052 33368 22735 67473 52982 10780 78683 63374 31446 68693 73409 61772 28604 36547 54591 70983 41560 12174 16778 12960 83191 27978 24192 22059 75590 20633 59373 10678 18772 17928 10917 11784 63043 51026 36503 53567 36759 68261 37444 86022 64528 38743 9709 30109 3889 51356 95341 5026 85039 48064 28214 90798 10065 46867 64767 36710 22769 4719 64188 32511 11924 29883 77213 56426 32147 21875 81385 18915 70621 40278 26945 29586 67208 52433 38331 71101 81116 51764 82650 19752 53196 9347 33401 96125 23802 75290 22003 53154 52748 87196 37447 1844 26265 68643 42146 60532 21026 83829 7456 21693 39544 40435 58439 59386 50243 28946 16047 80084 59536 16355 77946 38569 79673 50587 63058 96064 89649 45905 64175 80070 26253 47069 71160 23664 76812 90948 37700 19681 41965 45366 47993 84723 45194 86627 39170 68663 55315 27426 41531 24075 81228 63717 934 17018 73465 91181 19239 59060 31904 36618 77614
对应输出应该为:
580
你的输出为:
541
我总举得这个测试用例有问题,我的代码如下:
import java.util.Scanner;
public class HackerAward {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int len = s.nextInt();
int a[] = new int[len];
for (int i = 0; i < len; i++) {
a[i] = s.nextInt();
}
int result[] = new int[len];
for (int i = 0; i < len; i++) {
result[i] = 1;
}
for (int i = 0; i < len; i++) {
if (i - 1 >= 0) {
if (a[i - 1] < a[i])
result[i] = result[i - 1] + 1;
}
if (i + 1 <= len - 1) {
if (a[i + 1] < a[i]) {
result[i] = Math.max(result[i], result[i + 1] + 1);
int m = 1;
while (i - m >= 0 && a[i - m] == result[i - m + 1]) {
a[i - m]++;
m++;
}
}
}
}
int sum = 0;
for (int i = 0; i < len; i++) {
sum += result[i];
}
System.out.println(sum);
}
}
有知道的,麻烦帮忙看下~
说下我的思路:
依次遍历数组,每个元素都跟它的前一个和后一个比较,
和前一个比较,如果前面的小,则当前位置奖金加一;
和后一个比较,如果后面的小,则当前位置奖金加一之后,和当前位置比较,取较大值,
同时,加入当前位置加一之后和前一个位置相等,则前一个位置再次加一....
我明白了,是我粗心了,自己分析着分析知道哪里错了...
import java.util.Scanner;
public class HackerAward {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int len = s.nextInt();
int a[] = new int[len];
for (int i = 0; i < len; i++) {
a[i] = s.nextInt();
}
int result[] = new int[len];
for (int i = 0; i < len; i++) {
result[i] = 1;
}
for (int i = 0; i < len; i++) {
if (i - 1 >= 0) {
if (a[i - 1] < a[i])
result[i] = result[i - 1] + 1;
}
if (i + 1 <= len - 1) {
if (a[i + 1] < a[i]) {
result[i] = Math.max(result[i], result[i + 1] + 1);
int m = 1;
while (i - m >= 0 && result[i - m] == result[i - m + 1]
&& a[i - m] > a[i - m + 1]) {
result[i - m]++;
m++;
}
}
}
}
int sum = 0;
for (int i = 0; i < len; i++) {
sum += result[i];
}
System.out.println(sum);
}
}
这个是正确的程序
其实还是不对,我明明输出正确了,但是系统判定我没有输出,我郁闷了
这是一条镜像帖。来源:北邮人论坛 / java / #50256同步于 2016/5/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
请问大家,这个题我的代码哪里有问题吗?
nijian81
2016/5/13镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。