现在的位置: 首页 > 综合 > 正文

HDOJ-1164-Eddy’s research I 解题报告

2018年04月21日 ⁄ 综合 ⁄ 共 605字 ⁄ 字号 评论关闭

       分解质因数水题。题意:所有数都能够被分解成质因数的乘积,现在给你一个数,请输出这个数的质因数分解式。


       我的解题思路:就是分解质因数,只是格式不太好处理而已。除了第一个质因数前面不要输出*,其他质因数前面都输出一个*就可以了。


       我的解题代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <climits>
#include <algorithm>

using namespace std;

int num;

void Factor(int x);

int main()
{
    while (~scanf("%d", &num))
    {
        Factor(num);
    }
    return 0;
}

void Factor(int x)
{
    int cnt = (int)sqrt(x + 0.5) + 1;
    bool isfirst = true;
    for (int i=2; i<cnt; ++i)
    {
        if (x % i == 0)
        {
            if (isfirst)
            {
                printf("%d", i);
                x /= i;
                isfirst = false;
            }
            while (x % i == 0)
            {
                printf("*%d", i);
                x /= i;
            }
        }
    }
    if (x > 1)
    {
        if (isfirst)
        {
            printf("%d", x);
        }
        else
        {
            printf("*%d", x);
        }
    }
    putchar('\n');
    return;
}

抱歉!评论已关闭.