1059. Prime Factors (25)

时间限制
100 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 contains one test case which gives a positive integer N in the range of long int.

Output Specification:

Factor N in the format N = 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:

  1. 97532468

Sample Output:

  1. 97532468=2^2*11*17*101*1291
  2.  
  3. 思路
  4.  
  5. 按指定格式打印一个数的所有素(质)数因子。
    1.先初始化建立一个素数表。
    2.2开始,不断用素数i除输入的数num,能够整除说明满足条件,用cnt记录被整除的次数。
    3.对于第一个除数的输出进行判断来决定在除数前面是否加"*"
    4.cnt > 2就在当前除数后加"^cnt"
    5.重复2.3.4直至num < 2
    注:对于1要进行特殊处理,不然输出为"1=",正确输出应该是"1=1"
    代码
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. vector<int> isPrime(666666,1);
  6.  
  7. void Init()
  8. {
  9. for(int i = 2; i * i < 666666; i++)
  10. {
  11. for(int j = 2; j*i < 666666; j++)
  12. {
  13. isPrime[i * j] = 0;
  14. }
  15. }
  16. }
  17.  
  18. int main()
  19. {
  20. Init();
  21. long long num;
  22. cin >> num;
  23. cout << num <<"=";
  24. if(num == 1)
  25. cout << 1;
  26. bool first = true;
  27. for(int i = 2; num >= 2; i++)
  28. {
  29. int cnt = 0;
  30. if(isPrime[i] == 1 && num % i == 0)
  31. {
  32. while(num % i == 0)
  33. {
  34. cnt++;
  35. num = num/i;
  36. }
  37. if(first)
  38. first = false;
  39. else
  40. cout << "*";
  41. cout << i;
  42. if(cnt > 1)
  43. cout << "^" << cnt;
  44. }
  45. }
  46. }

  

PAT1059:Prime Factors的更多相关文章

  1. pat1059. Prime Factors (25)

    1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

  2. PAT-1059 Prime Factors (素数因子)

    1059. Prime Factors Given any positive integer N, you are supposed to find all of its prime factors, ...

  3. [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. 这道题跟 ...

  4. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  5. PAT 1059. Prime Factors (25) 质因子分解

    题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...

  6. 2014辽宁ACM省赛 Prime Factors

    问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB [提交][状态][论坛] 题目描写叙述 I'll give you a number , please te ...

  7. A1059. Prime Factors

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

  8. PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...

  9. PAT 1059 Prime Factors[难]

    1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...

随机推荐

  1. nginx 的编译参数详解

    内容有些多,一眼看来难免头昏脑胀,但坚持看完,相信你一定会有所收获. nginx参数: --prefix= 指向安装目录 --sbin-path 指向(执行)程序文件(nginx) --conf-pa ...

  2. Java进阶(二十二)使用FileOutputStream写入文件

    Java使用FileOutputStream写入文件 绪 在Java中,文件输出流是一种用于处理原始二进制数据的字节流类.为了将数据写入到文件中,必须将数据转换为字节,并保存到文件.请参阅下面的完整的 ...

  3. STL - 容器共性机制研究

    C++模板是容器的概念. 理论提高:所有容器提供的都是值(value)语意,而非引用(reference)语意.容器执行插入元素的操作时,内部实施拷贝动作.所以STL容器内存储的元素必须能够被拷贝(必 ...

  4. DataLoad命令

    Dataload常用命令 Dataload命令符 说明 Tab  或\{tab} 键盘Tab键,下一个单元 *UP  或\{UP} 键盘上 *DN  或\{DOWN} 键盘下 *LT  或\{LEFT ...

  5. 【cocos 2d-x】VS2013+cocos2d-x3.3Final+Adriod交叉编译环境配置(超详细版)

    本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.com ...

  6. 【cocos 2d-x】VS2012+win7+cocos2d-x3.0beta2开发环境配置

    本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.com ...

  7. 【编程练习】快速select算法的实现

    代码来自: http://blog.csdn.net/v_JULY_v 算法思想: // Quick_select.cpp : 定义控制台应用程序的入口点. // #include "std ...

  8. 【52】java多线程剖析

    线程的状态: 线程共有下面4种状态: 新建状态(New): 新创建了一个线程对象,当你用new创建一个线程时,该线程尚未运行. 就绪状态(Runnable): 线程对象创建后,其他线程调用了该对象的s ...

  9. 【Qt编程】3D迷宫游戏

    说起迷宫想必大家都很熟悉,个人感觉迷宫对人的方向感是很大的考验,至少我的方向感是不好的,尤其是在三维空间中.由于这段时间帮导师做项目用到了三维作图,便心血来潮想做个三维迷宫玩玩.要想画出三维的迷宫游戏 ...

  10. obj-c编程11:内存管理和ARC(自动引用计数)

    乖乖隆地洞,这篇文章内容可是不得了,内存管理哦!首先,这个要是搞不明白,你就等着进程莫名其妙的挂死,或是疯狂申请内存却不释放,结果被OS杀死,不管是"自杀"还是"他杀&q ...