cjweffort

博客园 首页 联系 订阅 管理

http://ac.jobdu.com/problem.php?cid=1040&pid=49

题目描述:

给定一个数n,要求判断其是否为素数(0,1,负数都是非素数)。

输入:

测试数据有多组,每组输入一个数n。

输出:

对于每组输入,若是素数则输出yes,否则输入no。

样例输入:
13
// 题目50:素数判定.cpp: 主项目文件。

#include "stdafx.h"
#include <cstdio>
#include <math.h>

bool isPrime(int n)
{
	if(n<=1)
		return false;
	int tt=(int)sqrt(1.0*n);
	for(int i=2;i<=tt;i++)
		if(n%i==0)
			return false;
	return true;
}

int main()
{
    int n;
	while(scanf("%d",&n)!=EOF)
	{
		if(isPrime(n))
			printf("yes\n");
		else
			printf("no\n");
	}
    return 0;
}


posted on 2013-03-06 11:30  cjweffort  阅读(239)  评论(0编辑  收藏  举报