patA1059 Prime Factors

这个问题叫做质因子分解,花了大概两个小时写对了。这道题细节挺多的,书上提到了几点,有一个很容易错的点就是n一开始要先用一个变量保存起来,不保存的话后面有点麻烦,所以建议还是先保存起来。因为过程中要不断的改变n,最后还要打印出n,如果n为1的话还要特殊处理。当然也可以一开始处理打印,不过我觉得和思维颠倒有些难受。一个很重要的问题就是打印的素数表需要多大,这个我也不清楚怎么办,所以先写出来看,随机应变,如果不能完全覆盖的话再往上加,最后完全覆盖了那个样例。最后解决的细节是当我输入2的时候一开始只探测到根号2导致问题没有解。其实根本没必要,因为有break来控制着for循环的结束条件。所以i<num的所有数字都正常输出就好。

一个很重要的思想就是设计结构体以及结构体数组,这个思想还是挺重要的。

#include<cstdio>
#include<math.h>
const int maxn = 19999900;
using namespace std;
struct factor
{
    int x;
    int cnt = 0;
}fac[10];
int num = 0;
bool p[10005] = { 0 };
int prime[100005];
void findprime()//获取素数表
{
    int i, j;
    for (i = 2; i *i< maxn; i++)
    {
        if (p[i] == false)
        {
            prime[num] = i;
            num++;
            for (j = i + i; j *j < maxn; j += i)
            {
                p[j] = true;
            }
        }
    }
}
int main()
{
    int n;
    int temp;
    scanf("%d", &n);
    temp = n;
    findprime();
    int i;
    int sum = 0;
    for (i = 0; i<num; i++)
    {
        int x = n;
        while (n % prime[i] == 0)
        {
            n /= prime[i];
            fac[sum].x = prime[i];
            fac[sum].cnt++;
        }
        if (x%prime[i] == 0)
        {
            sum++;
        }
        if (n == 1)
            break;
    }
    if (n != 1)
    {
        fac[sum].x = n;
        fac[sum].cnt = 1;
    }
    printf("%d=", temp);
    if (temp == 1)
    {
        printf("1\n");
    }
    else
        for (i = 0; i < sum; i++)
        {
            if (i != sum - 1 && fac[i].cnt > 1)//不是最后一个
            {
                printf("%d^%d*", fac[i].x, fac[i].cnt);
            }
            else if (i != sum - 1 && fac[i].cnt == 1)
            {
                printf("%d*", fac[i].x);
            }
            else if (i == sum - 1 && fac[i].cnt == 1)//是最后一个且cnt只有一个
            {
                printf("%d\n", fac[i].x);
            }
            else//最后一个cnt有两个
                printf("%d^%d\n", fac[i].x, fac[i].cnt);
        }
}

原文地址:https://www.cnblogs.com/legendcong/p/10540452.html

时间: 2024-08-11 13:24:01

patA1059 Prime Factors的相关文章

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟之前LeetCode的那道Ugly Number II 丑陋数之二基本没有啥区别,具体讲解可参见那篇,代码如下: class Solution { public: int getKthMagicNumber(int k) { vector<int> res(1, 1); int i3 = 0, i

PAT 1059. Prime Factors (25)

1059. Prime Factors (25) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive inte

1059. Prime Factors (25)

时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1* p2^k2 *…*pm^km. Input Specification: Each input file contain

2014辽宁ACM省赛 Prime Factors

问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 28 [提交][状态][论坛] 题目描述 I'll give you a number , please tell me how many different prime factors in this number. 输入 There is multiple test cases , in each test case there is only one line contain

PAT1059. Prime Factors

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive integer N in the range of lon

pat1059. Prime Factors (25)

1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specificatio

A1059. Prime Factors (25)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive integer N in the range of lon

1059 Prime Factors

题意: 给出一个int型正整数N,要求把N分解成若干个质因子,如N=97532468,则把N分解成:97532468=2^2*11*17*101*1291.质因子按增序输出,如果某个质因子的幂是1,则1不输出. 思路:质因子分解的基础题. 首先,定义如下因子的结构体,用于存放最终的结果.因为N是一个int范围的正整数,由于2*3*5*7*11*13*17*19*23*29>INT_MAX,也就是说,任意一个int型整数,可分解出来的不同的质因子的个数不会超过10个,因此,数组只要开到10就够了.

质因子分解——Prime Factors

先上原理 对于一个非素数来说 1,所有质因子小于等于sqrt(n) 2,只存在一个大于sqrt(n)的质因子,其他质因子都小于sqrt(n) 至于证明,可以用反证法. 若是有多余一个大于sqrt(n)的质因子,这些因子的乘积..... 下面上代码 这里借助一个结构体,当然你也可以用数组 struct factor { int x;//记录素因子 int cnt;//素因子的个数 }fac[10]; 2 3 5 7 11 13 17 19 23 29 以上10个素数的乘积已经超过了int的表示范围