美团笔试 第二题
老哥们帮我看看为啥只过了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];这个的问题,直接用常量接输入的值就行,用数组就爆
可能的原因
超时
相关推荐
04-03 21:40
重庆邮电大学 Web前端
smile丶snow:项目完成时间要写一个大概的区间,自己顺延一下就行。感觉ai对话的放第一个比较好。可以自己编一些场景或者找ai编一个场景。就是你为什么要写这个仿DeepSeek对话应用。比如你自己有很多文档,这个ai可以基于你自己的文档回答之类的。个人建议~具体看你自己。
还有项目中用到那些更好让ai coding的方法也可以写一下,毕竟现在ai大跃进… 点赞 评论 收藏
分享
点赞 评论 收藏
分享
05-25 18:01
华南理工大学 算法工程师 点赞 评论 收藏
分享
查看1道真题和解析