C 当范围(x!=y)之间没有素数时,如何添加另一个事例
下面是打印间隔x和y(包括x和y)之间的素数的C代码。 假设x是2,y是2,那么我在第1行为它添加了一条语句 类似地,我想给出另一个这样的陈述,但在这个陈述中,x和y不应该等于,例如x=8,y=10。我该怎么做 测试用例 输入1 2. 十一, 输出1 235711C 当范围(x!=y)之间没有素数时,如何添加另一个事例,c,C,下面是打印间隔x和y(包括x和y)之间的素数的C代码。 假设x是2,y是2,那么我在第1行为它添加了一条语句 类似地,我想给出另一个这样的陈述,但在这个陈述中,x和y不应该等于,例如x=8,y=10。我该怎么做 测试用例 输入1 2. 十一, 输出1 235711 #include<stdio.h> #include<math.h> void main() { int num, x, y, i, count; scanf("%d\n%d", &x
#include<stdio.h>
#include<math.h>
void main()
{
int num, x, y, i, count;
scanf("%d\n%d", &x, &y);
if (x<0 || y<0)
{
printf("\nNegative integers cannot be prime");
}
else if (x==y)
{
printf("\nThere are no prime numbers in the range %d to %d", x, y); //Line1
}
else
{
for (num=x; num<=y; num++)
{
count=0;
for(i=2; i<=num/2; i++)
{
if (num%i==0)
{
count++;
break;
}
}
if (count==0 && num!=1)
printf("\n%d ", num);
}
}
}
输入2
3.
-十,
输出2
负整数不能是素数
输入3
0
一,
输出3
在0到1的范围内没有素数
输入4
1.
一,
输出4
在1到1的范围内没有素数
输入5
1.
十一,
输出5
235711
#include<stdio.h>
#include<math.h>
void main()
{
int num, x, y, i, count;
scanf("%d\n%d", &x, &y);
if (x<0 || y<0)
{
printf("\nNegative integers cannot be prime");
}
else if (x==y)
{
printf("\nThere are no prime numbers in the range %d to %d", x, y); //Line1
}
else
{
for (num=x; num<=y; num++)
{
count=0;
for(i=2; i<=num/2; i++)
{
if (num%i==0)
{
count++;
break;
}
}
if (count==0 && num!=1)
printf("\n%d ", num);
}
}
}
#包括
#包括
void main()
{
int num,x,y,i,count;
scanf(“%d\n%d”、&x和&y);
如果(x你需要这个:
#include <stdio.h>
#include <math.h>
void main()
{
int num, x, y, i, count;
int primecounter = 0;
scanf("%d\n%d", &x, &y);
if (x < 0 || y < 0)
{
printf("\nNegative integers cannot be prime\n");
}
else
{
for (num = x; num <= y; num++)
{
count = 0;
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
count++;
break;
}
}
if (count == 0 && num != 1)
{
printf("\n%d ", num);
primecounter++;
}
}
if (primecounter == 0)
{
printf("There are no prime numbers in the range %d to %d\n", x, y);
}
}
}
#包括
#包括
void main()
{
int num,x,y,i,count;
int primecounter=0;
scanf(“%d\n%d”、&x和&y);
if(x<0 | | y<0)
{
printf(“\n负整数不能是素数\n”);
}
其他的
{
对于(num=x;num这里,我向您展示了一个经过修改的代码来处理您要求的案例。
需要注意的几件事-
- 不需要将案例
x==y
与第三个案例分开处理
- 素性测试由一个函数
checkPrime
处理。它被稍微更改为在O(sqrt(num))
中执行,而不是在O(num)
中执行
main
变量中的count
现在与代码中的用法不同。它计算x
和y
之间的素数(包括)
修改代码-
#include<stdio.h>
#include<math.h>
int checkPrime(int num)
{
if(num==1)
return 0;
int i,sq=(int)sqrt(num);
for(i=2;i<=sq;i++)
if(num%i==0)
return 0;
return 1;
}
int main()
{
int num, x, y, i, count;
scanf("%d\n%d", &x, &y);
// this is to ensure that x<=y
if(x>y)
{
int temp=x;
x=y;
y=temp;
}
if (x<0 || y<0)
{
printf("Negative integers cannot be prime\n");
}
else
{
count=0;
for (num=x; num<=y; num++)
{
if(checkPrime(num)){
count++;
printf("%d ",num);
}
}
if(count==0)
printf("There are no prime numbers in the range %d to %d", x, y);
printf("\n");
}
return 0;
}
#包括
#包括
int checkPrime(int num)
{
如果(num==1)
返回0;
inti,sq=(int)sqrt(num);
对于(i=2;iI不理解:在x=2和y=2的情况下,2是素数,对吗?因此,else if
分支是错误的。没有简单的测试来确定给定间隔中是否有素数;您必须检查它们。可能保留一个找到素数的计数器,如果在间隔中没有找到,则打印消息。另外,在区间[2,2]中有一个素数。您的代码表示没有素数…我刚刚添加了强制转换案例。这就是我要实现的,您不需要检查(x==y)
。这将在任何给定的范围内进行处理。只需引入一个标志,指示您在循环过程中是否找到了一些素数,如果这仍然是false
,请打印您的消息。问题:为什么为(i=2;iYes,但我在问它。否。问自己这个问题:sqrt(num)有多少次
executed?好的,这是关于重复计算相同的值。没错。但是调用sqrt
一次是可以的,它肯定比您的新解决方案好。另一方面,您的初始解决方案可以通过智能编译器进行优化。