问题描述:
http://acm.jlu.edu.cn/joj/showproblem.php?pid=2771&contestid=199
Welcome to 2012 Jilin Province Collegiate Programming Contest.
Year 2012 is a special year, and the 2012 is also a special number. For example,2012= 2*2*503, here 2 and 503 are both prime number.
As we know, A prime number (or a prime) is a natural number greater than 1 that
has no positive divisors other than 1 and itself.
This time, we should find out the numbers that can be divided like 2012. That is to
say, for a positive number n, can be written n=p1*p1*p2, here p1 and p2 are
different prime numbers.
Input
This problem has no input
Output
You should output all the numbers that satisfied the rules before. There numbers
are increasing order and less than 3000.
Sample Output
12
18
……
2012
……
Not all the numbers are listed in the sample. There are just some examples. The
ellipsis expresses what you should calculate.
答案代码:
#include<iostream>
#include<cmath>
using namespace std;
//判断素数函数
bool isPrime(int n)
{
int i=2;
int upper=sqrt(float(n));
while(i<=upper)
{
if(n%i==0)
{
return false;
}
i++;
}
return true;
}
//主函数
int main()
{
int sn=12;
while(sn<=3000)
{
if((sn%(2*2)==0)&&(isPrime(sn/(2*2))))
{
cout<<sn<<endl;
}
int upper=sqrt(float(sn));
for(int i=3;i<upper;i=i+1)
{
if(isPrime(i)&&(sn%(i*i)==0)&&(i!=(sn/(i*i)))&&(isPrime(sn/(i*i))))
{
cout<<sn<<endl;
}
}
sn++;
}
return 0;
}