本帖最后由 jackz007 于 2022-10-16 20:10 编辑 #include<stdio.h>
int isprime(long number)
{
int prime = 0 , div ;
if(number > 1) {
if(number % 2 && number % 3 && number % 5 && number % 7) {
for(prime = 1 , div = 3 ; (div * div) <= number ; div += 2) {
if (number % div == 0) {
prime = 0 ;
break ;
}
}
} else if(number == 2 || number == 3 || number == 5 || number == 7) {
prime = 1 ;
}
}
return prime ;
}
int main(void)
{
long d[2000][2] , i , j , n = 0 , sum ;
for(n = 0 ;; n ++) if((scanf("%ld%ld" , & d[n][0] , & d[n][1])) == EOF) break ;
for(i = 0 ; i < n ; i ++) {
for(j = d[i][0] + 1 , sum = 0 ; j < d[i][1] ; j ++) if(isprime(j)) sum += j ;
printf("%ld\n" , sum) ;
}
}
编译、运行实况:D:\[00.Exerciese.2022]\C>g++ -o x x.c
D:\[00.Exerciese.2022]\C>x
2 10
23 90
39 1224
^Z
15
863
111390
D:\[00.Exerciese.2022]\C>
这是我写的版本:#include<stdio.h>
int main(void)
{
long d[2000][2] , i , j , k , n , sum ;
for(n = 0 ;; n ++) if((scanf("%ld%ld" , & d[n][0] , & d[n][1])) == EOF) break ;
for(i = 0 ; i < n ; i ++) {
for(j = d[i][0] + 1 , sum = 0 ; j < d[i][1] ; j ++) {
for(k = 2 ; k < j ; k ++) if(! (j % k)) break ;
if(k == j) sum += j ;
}
printf("%ld\n" , sum) ;
}
}
|