zoukankan      html  css  js  c++  java
  • 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 integer N in the range of long int.

    Output Specification:

    Factor N in the format = p1​​^k1​​*p2​​^k2​​**pm​​^km​​, where pi​​'s are prime factors of N in increasing order, and the exponent ki​​ is the number of pi​​ -- hence when there is only one pi​​, ki​​ is 1 and must NOT be printed out.

    Sample Input:

    97532468
    

    Sample Output:

    97532468=2^2*11*17*101*1291

     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include <climits>
     3 #include<iostream>
     4 #include<vector>
     5 #include<queue>
     6 #include<map>
     7 #include<set>
     8 #include<stack>
     9 #include<algorithm>
    10 #include<string>
    11 #include<cmath>
    12 using namespace std;
    13 bool Prime[100000];
    14 void Make_Prime(int N)
    15 {
    16     fill(Prime, Prime + N+1, true);
    17     for (int i = 2; i<N;i++)
    18     {
    19         if (!Prime[i])continue;
    20         for (int j = 2; i * j <N; j++)
    21             Prime[i * j] = false;
    22     }
    23 }
    24 
    25 int main()
    26 {
    27     Make_Prime(100000);
    28     long long N;
    29     cin >> N;
    30     cout << N << "=";
    31     if (N == 1)
    32         cout << N;
    33     bool have = false;
    34     for (int i = 2; i < N; i++)
    35     {
    36         bool flag = false;
    37         int count = 0;
    38         while(Prime[i] && N % i == 0)
    39         {
    40             flag = true;
    41             count++;
    42             N /= i;
    43         }
    44         if (flag)
    45         {
    46             if (have)cout << "*";
    47             cout << i;
    48             if (count > 1)
    49                 cout << "^" << count;
    50             have = true;
    51         }
    52     }
    53     if (N > 1)
    54         if (have)cout << "*" << N;
    55         else cout << N;
    56 }
    View Code


  • 相关阅读:
    css | js 实现扩展卡片小demo
    ESLint如何配置
    (js描述的)数据结构[哈希表1.3](10)
    (js描述的)数据结构[哈希表1.2](9)
    VSCode——自定义VSCode背景图片
    VSCode 初次写vue项目并一键生成.vue模版
    (js描述的)数据结构[哈希表1.1](8)
    (js描述的)数据结构[字典](7)
    Vue 实战项目: 硅谷外卖(1)
    脑残式网络编程入门(六):什么是公网IP和内网IP?NAT转换又是什么鬼?
  • 原文地址:https://www.cnblogs.com/57one/p/12054818.html
Copyright © 2011-2022 走看看