hdu2012

renjianbuchai_233 2016-02-06 01:46:10
素数判定
Problem Description
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。
Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。
Sample Input
0 1
0 0
Sample Output
OK

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,i,t,j,x;
bool c = true;
while(scanf("%d%d",&a,&b) != EOF)
{
if(a == 0 && b == 0)
break;
if(a > b)
{
t = a;
a = b;
b = t;
}

for(i = a;i <= b;i++)
{
x = i * i + i + 41;
for(j = 2;j <= int(sqrt(i));j++)
{
if(x % j != 0)
c = true;
else
{
printf("Sorry\n");
break;
}
}
if(c == true)
printf("OK\n");
}
}
return 0;
}
求纠错
...全文
251 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2016-02-08
  • 打赏
  • 举报
回复
计算错误,41<= n^2+n+41<= 2591 考虑对称性,(-x)^2 -x +41 = (x+1)^2+(x+1)+41 这个其实只要检测一半区域就够了
renjianbuchai_233 2016-02-08
  • 打赏
  • 举报
回复
引用 10 楼 lm_whales 的回复:
-39<= n<=50,41 =< n^2+n+41 <= 3041 这个范围的素数很少,筛法的话,非常快
应该不需要查表才对,当然就是直接判断,也没问题才对,不会很慢的

f(n+1) -f(n) = 2(n+1) 这是个二阶等差数列,循环的话,也很快

但是除了打表其他方法都不能ac
renjianbuchai_233 2016-02-07
  • 打赏
  • 举报
回复
引用 4 楼 paschen 的回复:
c 的初始值是否应该是false,否则c一直为true
我那个Java的太阳系模型的问题能帮我看看嘛,谢谢了
renjianbuchai_233 2016-02-07
  • 打赏
  • 举报
回复


引用 4 楼 paschen 的回复:
c 的初始值是否应该是false,否则c一直为true

还是一样
lm_whales 2016-02-07
  • 打赏
  • 举报
回复
-39<= n<=50,41 =< n^2+n+41 <= 3041 这个范围的素数很少,筛法的话,非常快 应该不需要查表才对,当然就是直接判断,也没问题才对,不会很慢的 f(n+1) -f(n) = 2(n+1) 这是个二阶等差数列,循环的话,也很快
renjianbuchai_233 2016-02-07
  • 打赏
  • 举报
回复
引用 7 楼 paschen 的回复:
你试下这种,我没调试


#include<stdio.h>
#include<math.h>
int main()
{
	int a, b, i, t, j, x;
	bool c = true;
	while (scanf("%d%d", &a, &b) != EOF)
	{
		if (a == 0 && b == 0)
			break;
		if (a > b)
		{
			t = a;
			a = b;
			b = t;
		}

		for (i = a; i <= b; i++)
		{
			x = i * i + i + 41;
			for (j = 2; j <= int(sqrt(x)); j++)
			{
				if (x % j == 0)
				{
					c = false;
					printf("Sorry\n");
					break;
				}
			}
		}
		if (c == true)
			printf("OK\n");
	}
	return 0;
}
#include<stdio.h> #include<string.h> int main(void) { int m, n; int x[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1 }; while (scanf("%d%d", &m, &n), m || n) { for (m += 39, n += 39; x[m] && m <= n ; m++); puts(m > n ? "OK" : "Sorry"); } return 0; } 网上的只有这种打表的能a,我也不知道为什么
renjianbuchai_233 2016-02-07
  • 打赏
  • 举报
回复
引用 7 楼 paschen 的回复:
你试下这种,我没调试




#include<stdio.h>
#include<math.h>
int main()
{
int a, b, i, t, j, x;
bool c = true;
while (scanf("%d%d", &a, &b) != EOF)
{
if (a == 0 && b == 0)
break;
if (a > b)
{
t = a;
a = b;
b = t;
}

for (i = a; i <= b; i++)
{
x = i * i + i + 41;
for (j = 2; j <= int(sqrt(x)); j++)
{
if (x % j == 0)
{
c = false;
printf("Sorry\n");
break;
}
}
}
if (c == true)
printf("OK\n");
}
return 0;
}

paschen 2016-02-07
  • 打赏
  • 举报
回复
你试下这种,我没调试


#include<stdio.h>
#include<math.h>
int main()
{
	int a, b, i, t, j, x;
	bool c = true;
	while (scanf("%d%d", &a, &b) != EOF)
	{
		if (a == 0 && b == 0)
			break;
		if (a > b)
		{
			t = a;
			a = b;
			b = t;
		}

		for (i = a; i <= b; i++)
		{
			x = i * i + i + 41;
			for (j = 2; j <= int(sqrt(x)); j++)
			{
				if (x % j == 0)
				{
					c = false;
					printf("Sorry\n");
					break;
				}
			}
		}
		if (c == true)
			printf("OK\n");
	}
	return 0;
}
paschen 2016-02-06
  • 打赏
  • 举报
回复
c 的初始值是否应该是false,否则c一直为true
renjianbuchai_233 2016-02-06
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
for(j = 2;j <= int(sqrt(i));j++) 应该是 for(j = 2;j <= int(sqrt(x));j++) 吧
#include<stdio.h> #include<math.h> int main() { int a,b,i,t,j,x; bool c = true; while(scanf("%d%d",&a,&b) != EOF) { if(a == 0 && b == 0) break; if(a > b) { t = a; a = b; b = t; } for(i = a;i <= b;i++) { x = i * i + i + 41; for(j = 2;j <= int(sqrt(x));j++) { if(x % j != 0) c = true; else { printf("Sorry\n"); break; } } } if(c == true) printf("OK\n"); } return 0; }
renjianbuchai_233 2016-02-06
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
for(j = 2;j <= int(sqrt(i));j++) 应该是 for(j = 2;j <= int(sqrt(x));j++) 吧
a不了
paschen 2016-02-06
  • 打赏
  • 举报
回复
for(j = 2;j <= int(sqrt(i));j++) 应该是 for(j = 2;j <= int(sqrt(x));j++) 吧

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧