2012素数判定c++

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<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
	int x, y;
	while (cin >> x >> y && x >= -39 && y <= 50 && x < y )
	{
		if (x==0&&y==0)
		{
			break;
		}
		else
		{
			int flag = 0;
			for (int i = x; i <= y; i++)
			{
				int num = i*i + i + 41;
				for (int j = 2; j <= num / 2; j++)
				{
					if (num%j == 0)
					{
						flag = 1;
						break;
					}
				}
			}
			if (flag == 1)
			{
				cout << "Sorry" << endl;
			}
			else
			{
				cout << "OK" << endl;
			}
		}
		
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41274723/article/details/89508349