程序设计问题
大佬们,能帮我看看我哪里错了吗,提交到oj后提示答案错误诶https://s1.ax1x.com/2022/10/16/xBBdmR.png
样例输入:
2 10
23 90
39 1224
#include<stdio.h>
int isprime(int number);
int main(void)
{
int a, b;
long sum = 0;
while (scanf("%d%d",&a,&b) != EOF)
{
sum = 0;
for (int i = a + 1; i < b; i++)
{
if (isprime(i))
{
sum += i;
}
}
printf("%ld\n", sum);
}
return 0;
}
int isprime(int number)
{
int isprime = 0; //默认不是素数
for (int div = 2; (div * div) <= number; div++)
{
if (number % div == 0)
{
return isprime;
}
}
} 本帖最后由 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 , i , j , n = 0 , sum ;
for(n = 0 ;; n ++) if((scanf("%ld%ld" , & d , & d)) == EOF) break ;
for(i = 0 ; i < n ; i ++) {
for(j = d + 1 , sum = 0 ; j < d ; j ++) if(isprime(j)) sum += j ;
printf("%ld\n" , sum) ;
}
}
编译、运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
2 10
23 90
39 1224
^Z
15
863
111390
D:\\C>
这是我写的版本:
#include<stdio.h>
int main(void)
{
long d , i , j , k , n , sum ;
for(n = 0 ;; n ++) if((scanf("%ld%ld" , & d , & d)) == EOF) break ;
for(i = 0 ; i < n ; i ++) {
for(j = d + 1 , sum = 0 ; j < d ; j ++) {
for(k = 2 ; k < j ; k ++) if(! (j % k)) break ;
if(k == j) sum += j ;
}
printf("%ld\n" , sum) ;
}
}
页:
[1]