ACM HDU Primes(素数判断)
2014-03-28 19:29
363 查看
Problem Description
Writea program to read in a list of integers and determine whether or not eachnumber is prime. A number, n, is prime if its only divisors are 1 and n. Forthis problem, the numbers 1 and 2 are not considered primes.
Input
Eachinput line contains a single integer. The list of integers is terminated with anumber<= 0. You may assume that the input contains at most 250 numbers andeach number is less than or equal to 16000.
Output
Theoutput should consists of one line for every number, where each line firstlists the problem number, followed by a colon and space, followed by"yes" or "no".
Sample Input
1
2
3
4
5
17
0
Sample Output
1: no
2: no
3: yes
4: no
5: yes
6: yes
/************************************************************************
简单数论,判断素数,,,
**************************************************/
#include<stdio.h>
bool prime(int n)
{
if(n<=2)
return false;// 这题好奇葩, 1,2不算素数
for(int i = 2;i*i<=n;i++)
if(n%i==0)
return false;
return true;
}
int main()
{
int n,count = 0;// 注意输出时前面的数字是计数的,而不是输入的数字
while(scanf("%d",&n)&&n>0)
{
count++;
if(prime(n))
printf("%d: yes\n",count);
else
printf("%d: no\n",count);
}
return 0;
}
// 虽然很简单,但是想记下来,慢慢积累。。
Writea program to read in a list of integers and determine whether or not eachnumber is prime. A number, n, is prime if its only divisors are 1 and n. Forthis problem, the numbers 1 and 2 are not considered primes.
Input
Eachinput line contains a single integer. The list of integers is terminated with anumber<= 0. You may assume that the input contains at most 250 numbers andeach number is less than or equal to 16000.
Output
Theoutput should consists of one line for every number, where each line firstlists the problem number, followed by a colon and space, followed by"yes" or "no".
Sample Input
1
2
3
4
5
17
0
Sample Output
1: no
2: no
3: yes
4: no
5: yes
6: yes
/************************************************************************
简单数论,判断素数,,,
**************************************************/
#include<stdio.h>
bool prime(int n)
{
if(n<=2)
return false;// 这题好奇葩, 1,2不算素数
for(int i = 2;i*i<=n;i++)
if(n%i==0)
return false;
return true;
}
int main()
{
int n,count = 0;// 注意输出时前面的数字是计数的,而不是输入的数字
while(scanf("%d",&n)&&n>0)
{
count++;
if(prime(n))
printf("%d: yes\n",count);
else
printf("%d: no\n",count);
}
return 0;
}
// 虽然很简单,但是想记下来,慢慢积累。。
相关文章推荐
- ACM HDU Primes(素数判断)
- HDU 2161 Primes 判断素数
- HDU 2161 Primes 判断素数
- [HDU 2161] Primes(素数判断)
- HDU 2161Primes(判断是否是素数)
- HDU 2161 Primes (判断素数的水题)
- (step7.2.2)hdu 2161(Primes——判断是否是素数)
- http://acm.hdu.edu.cn/showproblem.php?pid=1905 素数判断 + 二分
- (step7.2.2)hdu 2161(Primes——判断是否是素数)
- HDU 5901 Count primes(1e11以内的素数个数)
- ACM刷题之HDU————相遇周期(真·打表(素数筛选))
- [ACM] TOJ 1054 Jesse's Code (素数判断优化+全排列)
- hdu 5901 Count primes (大素数模板)
- hdu 2138 判断素数(Miller-Rabin算法)
- HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛
- 分拆素数和(http://acm.hdu.edu.cn/showproblem.php?pid=2098)
- ACM常用素数判断筛选法及其优化
- HDU 5512 Pagodas(gcd + 奇偶性判断)——2015ACM/ICPC亚洲区沈阳站
- [ACM] TOJ 1054 Jesse's Code (素数判断优化+全排列)
- HDU 5901 Count primes 2016年沈阳网络赛 (Lehmer素数计数)