PAT 1059. Prime Factors

反正知道了就是知道,不知道也想不到,很快

#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

inline void print_prime_k(long long p, long long k) {
       printf("%lld", p);
       if (k > 1) {
          printf("^%lld", k);
       }
}

int main() {
    long long n, on;
    scanf("%lld", &n);
    on = n;
    vector<long long> ps;
    vector<long long> count;

    long long i = 2;
    long long last = 0;
    while (n > 1) {
        while (n % i == 0) {
              n = n/i;
              if (i != last) {
                 ps.push_back(i);
                 count.push_back(0);
                 last = i;
              }
              count.back()++;
        }
        i++;
    }

    int len = ps.size();
    if (len > 0) {
       printf("%lld=", on);
       print_prime_k(ps[0], count[0]);
    } else {
      printf("%lld=%lld", on, on);
    }
    for (int i=1; i<len; i++) {
        printf("*");
        print_prime_k(ps[i], count[i]);
    }
    system("pause");
    return 0;
}

考虑输入是1的情况输出要为1=1

时间: 2024-10-09 20:29:06

PAT 1059. Prime Factors的相关文章

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

PAT:1059. Prime Factors (25) AC

#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; const int MAX=100010; //int型素数一定在这个范围内 int PrimeArr[MAX]; //素数表 int cnt=0; //素数表中素数个数 int x,x2; //输入的数字,x2是x的开平方数,x的因子只有从2到根号x+1 struct

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就够了.

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

[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

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

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