美团笔试 第二题
老哥们帮我看看为啥只过了77%
package test;
import java.util.*;
public class two {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
long[] a = new long[n];
long count = 0;
long sum = 0;
long l;
long r;
for (int i = 0; i < n; i++) {
a[i] = sc.nextLong();
sum += a[i];
if (a[i] == 0) {
count++;
}
}
while (sc.hasNext()) {
l = sc.nextLong();
r = sc.nextLong();
System.out.print(sum + l * count + " ");
System.out.print(sum + r * count + " ");
System.out.println();
}
}
}
package test;
import java.util.*;
public class two {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
long[] a = new long[n];
long count = 0;
long sum = 0;
long l;
long r;
for (int i = 0; i < n; i++) {
a[i] = sc.nextLong();
sum += a[i];
if (a[i] == 0) {
count++;
}
}
while (sc.hasNext()) {
l = sc.nextLong();
r = sc.nextLong();
System.out.print(sum + l * count + " ");
System.out.print(sum + r * count + " ");
System.out.println();
}
}
}
全部评论
把nextLong改成int
话说while循环是不是应该把q--!=0作为终止判断条件
把两个输出合在一起就过了,print耗时太大
q没有使用
不是所有0都能改的,超过q个的0不能改
输出挺浪费时间的,改成一行输出
我也卡在这好久,就是long[] a = new long[n];这个的问题,直接用常量接输入的值就行,用数组就爆
可能的原因
超时
相关推荐
10-29 19:45
吉林大学 Java
从零开始数:自我评价没有必要写,但是看起来你应该是学了csdiy的一些课程,可以在专业技能里面写上自己比较熟悉操作系统和计网,但如果你是找Java的话,把第一个项目换了吧,现在看起来有点四不像。
无论是黑马点评或者说做个轮子项目,刷题和八股也搞起来吧,而且也没必要等到寒假,最近就可以开始找,找到就偷偷实习呗,别被逮到就行了。 点赞 评论 收藏
分享
09-25 21:25
武汉理工大学 Java 点赞 评论 收藏
分享
点赞 评论 收藏
分享
查看23道真题和解析
