英文题:Eddy's research I

Title 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 <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
using namespace std;
#define Max 65535
int data[Max + 100],jj;
bool vis[Max + 100];


void yuchuli()
{
    jj=0;
    memset(vis,true,sizeof(vis));
    vis[1] = false;
    for(int i = 2; i<= Max ;i++)
    {
        if(vis[i] == true){
            data[jj++] = i;
            for(int j=i+i;j<=Max;j=j+i)
                vis[j] = false;
        }
    }
}

int main()
{
    yuchuli();
    //printf("%d\n",jj);
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        int ff =0;
        for(int i = 0; i<=jj && data[i]<=t ;i++){
            while(t%data[i]==0 && data[i]<=t) {
                t = t/data[i];
                if(ff++!=0) printf("*");
                printf("%d",data[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zjwsa/article/details/81174157