Brian Warehouse

Some birds aren`t meant to be caged, their feathers are just too bright... ...
posts - 40, comments - 16, trackbacks - 0, articles - 1

SGU 113 Nearly prime numbers

Posted on 2010-08-17 13:23 Brian 阅读(314) 评论(0)  编辑 收藏 引用 所属分类: SGU

Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if given number is nearly prime and “No” otherwise.

Input

Input file consists of N+1 numbers. First is positive integer N (1£N£10). Next N numbers followed by N. Each number is not greater than 109. All numbers separated by whitespace(s).

Output

Write a line in output file for each number of given sequence. Write “Yes” in it if given number is nearly prime and “No” in other case.
不用管Nearly prime numbers 到底是个什么数,总之是两个质数的乘积就对了。枚举的范围: 2 ~ 32000 (104.5 )
我的思路是: 首先把2~32000之间的所有素数都存放在一个数组里,然后当你输入一个数据时,先让其逐一模除这个数组里的素数,一旦模除结果为0,则计算出他们的商,再判断商是否也为素数。注意数据是两个数的平方的处理

SGU 113 Nearly prime numbers  - Icho - Brian Warehouse#include <stdio.h>
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse#include 
<math.h>
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
int prime[30000],M=0;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
int isP(int n) //判断是否为素数,非常精确而高效
SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
int i=2,t=sqrt(n);
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
if ((n != 2 && !(n % 2)) || (n != 3 && !(n % 3)) || 
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        (n 
!= 5 && !(n % 5)) || (n != 7 && !(n % 7)))
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
return 0;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
for (; i<=t; i++)
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
if (n%== 0)
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            
return 0;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
return 1;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse}

SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
int isNP(int n)
SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
int i=0,t=sqrt(n),r;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
for (; prime[i]<=t; i++// 平方在此处理
SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
    SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
if (n%prime[i] == 0// 模除试商
SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
        SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            r
=n/prime[i]; // 求商
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
            if (isP(r))
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse                
return 1;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        }

SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    }

SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
return 0;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse}

SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
int main()
SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
int i=3,N,m;    
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
for (prime[M++]=2; i<32000; i+=2)
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
if (isP(i))
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            prime[M
++]=i;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    scanf(
"%d",&N);
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
while (N--)
SGU 113 Nearly prime numbers  - Icho - Brian WarehouseSGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse{
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        scanf(
"%d",&m);
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse        
if (m==6)
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse            printf(
"Yes\n"); // 程序中唯一未解决的问题,望各路大牛指教!
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse
        else printf("%s\n",isNP(m) ? "Yes":"No");
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    }

SGU 113 Nearly prime numbers  - Icho - Brian Warehouse    
return 0;
SGU 113 Nearly prime numbers  - Icho - Brian Warehouse}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理