- 三倍数
究竟是哪里超时了
- 2024-6-13 17:38:49 @
public class P1005 {
static int len,q;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
len = sc.nextInt();//数组长度
q = sc.nextInt();//请求次数
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = sc.nextInt()%3;
}
for (int i = 0; i <q ; i++) {
int x = sc.nextInt() - 1;
int y = sc.nextInt() - 1;
System.out.println(function(a,x,y)?"YES":"NO");
}
}
private static Boolean function(int[] a,int x,int y) {
int Yushu = 0;
for (;x<=y;x++) {
Yushu += a[x];
}
if (Yushu % 3 == 0) return true;
else return false;
}
}
似乎是在大量数据的情况下会超时,目前能想到的可能就是for循环用太多了,但没想明白还能怎么修改,或者是背的思路```
0 comments
No comments so far...
Information
- ID
- 5
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 3
- Tags
- # Submissions
- 656
- Accepted
- 50
- Uploaded By