素数筛选

题目:

Problem D

Problem Description

Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to help him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .

Input

The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.

Output

You have to print a line in the output for each entry with the answer to the previous question.

Sample Input

11
9412

Sample Output

11
2*2*13*181
代码:

//该题与模板相比就只有一个地方要处理一下,就是组成拆分数的素数因子有可能是几个相同的因子
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 65535
int prime[MAX+5];
void fun()
{
    memset(prime,0,sizeof(prime));
    for(int i=2;i<=MAX/2;i++)
    {
        for(int j=i+i;j<=MAX;j+=i)
        {
            prime[j]=1;
        }
    } 
}
int main()
{
    fun();
    int x;
    while(scanf("%d",&x)!=EOF)
    {
        int i=2;
        int c=1;
        int t=x; 
        while(i<=x)
        {
             if(t%i==0 && !prime[i])
             {
                 if(c==1)
                 {
                     printf("%d",i);
                     c++;
                 }
                 else
                 {
                     printf("*%d",i);
                 }
                 t=t/i;//刚开始t/i被我写成t/2,结果一直不对 ,哎。。。又粗心了 
             }
             else
             {
                 i++;
             }
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

原文链接: https://www.cnblogs.com/zhangshu/archive/2011/05/31/2065242.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    素数筛选

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/26520

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午4:10
下一篇 2023年2月8日 上午4:10

相关推荐