2032:【例4.18】分解质因数(2032: [example 4.18] decomposition quality factor)

【题目描述】

把一个合数分解成若干个质因数乘积的形式(即求质因数的过程)叫做分解质因数。分解质因数(也称分解素因数)只针对合数。

输入一个正整数nn,将nn分解成质因数乘积的形式。

【输入】

一个正整数nn。

【输出】

分解成质因数乘积的形式。质因数必须由小到大,见样例。

【输入样例】

36

【输出样例】

36=2*2*3*3

【提示】

【数据范围】

对于所有数据,2≤n≤200002≤n≤20000。

#include<stdio.h>
#include<math.h>
int asd(int n)
{
    int a=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            a=0;
            break;
        }
    }
    return a;
}
int main()
{
    int n,y,i,j;
    scanf("%d", &n);
    y=n;
    i=2;
    printf("%d=",n);
    while(1)
    {
        if(asd(i)&&(y%i==0)) 
        {
            y/=i;
            if(y==1)
            {
                printf("%d\n", i);
                break;
            }
            else printf("%d*",i);
        }
        else i++;
    }
    return 0;
}
————————

[Title Description]

Decomposing a composite number into the form of the product of several prime factors (that is, the process of finding the prime factor) is called decomposing the prime factor. Decomposition prime factor (also known as decomposition prime factor) is only for composite numbers.

Enter a positive integer NN and decompose NN into the form of prime factor product.

[input]

A positive integer NN.

[output]

It is decomposed into the form of prime factor product. The quality factor must be from small to large, as shown in the example.

[input example]

36

[output example]

36=2*2*3*3

[tips]

[data range]

For all data, 2 ≤ n ≤ 200002 ≤ n ≤ 20000.

#include<stdio.h>
#include<math.h>
int asd(int n)
{
    int a=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            a=0;
            break;
        }
    }
    return a;
}
int main()
{
    int n,y,i,j;
    scanf("%d", &n);
    y=n;
    i=2;
    printf("%d=",n);
    while(1)
    {
        if(asd(i)&&(y%i==0)) 
        {
            y/=i;
            if(y==1)
            {
                printf("%d\n", i);
                break;
            }
            else printf("%d*",i);
        }
        else i++;
    }
    return 0;
}