我正在尝试提交 Spoj - Prime Intervals 的解决方案问题。但是我收到运行时错误 SIGXFSZ .据了解,它是由于超出文件大小而发生的。我用过 Sieve of Eratosthenes寻找素数的算法。我不明白我的代码有什么问题,这在过去 2 天一直困扰着我。请帮我提交。这是我的代码...
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
int main(){
int t, turn;
long i, l,u,k,j;
scanf("%d", &t);
/*Looping for t test cases*/
for(turn=0; turn<t; turn++){
scanf("%ld %ld", &l, &u);
bool arr[u-l+1];
/*Assigning whole array with true*/
memset(arr, true, u-l+1);
/*Sieve of Eratosthenes logic for assigning false to composite values*/
for(i=0; i<=(int)sqrt(u)-l; i++){
k=0;
j = i+l;
if(arr[i]==true){
while((j*j + k*j) <= u){
arr[(j*j + k*j) - l] = false;
k++;
}
}
}
/*Printing all the primes in the interval*/
for(i=0; i<u-l; i++){
if(arr[i]==true){
printf("%ld\n", i+l);
}
}
}
return 0;
}
测试输入:
2
2 10
2 100
输出:
2
3
5
7
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
最佳答案
我运行了发布的代码。结果远非正确。
大部分输出的数字都不是质数,没有检查最后一个数字是否在范围内,如第二组结果所示
结果如下:
1 <-- 1 test case
20 100 <-- range 20...100
20 <-- the outputs
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
注意:使用 1 作为范围的低端通常不会产生任何输出
这是另一个运行
输出应该是 5 7 11
1 <-- test cases
5 11 <-- range
5 <-- outputs
6
7
8
9
10
关于c - SIGXFSZ 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33741285/