美团笔试 第二题
老哥们帮我看看为啥只过了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];这个的问题,直接用常量接输入的值就行,用数组就爆
可能的原因
超时
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
11-18 20:04
泉州职业技术大学 算法工程师
专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了
把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。
现在是学校不是92就扣分的,没必要放前面。
然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历 点赞 评论 收藏
分享
11-08 21:07
门头沟学院 Java 点赞 评论 收藏
分享