1059. Prime Factors (25)

雨点打透心脏的1/2处 2021-11-22 04:56 293阅读 0赞

注意n=n这种情况

  1. // 1059.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include<iostream>
  4. #include<queue>
  5. #include<vector>
  6. #include<algorithm>
  7. using namespace std;
  8. queue<long int> prime;
  9. vector<pair<long int,long int> > col;
  10. void printElem(const pair<int,int> it)
  11. {
  12. if(it.second==1)
  13. {
  14. printf("%ld*",it.first);
  15. }
  16. else
  17. {
  18. printf("%ld^%ld*",it.first,it.second);
  19. }
  20. }
  21. int main()
  22. {
  23. long int n;
  24. freopen("1059.txt","r",stdin);
  25. while(scanf("%ld",&n)!=EOF)
  26. {
  27. prime.push(2);
  28. for(long int i=3;i<=sqrt(n*1.0);i++)
  29. {
  30. bool flag=true;
  31. for(long int j=2;j<=sqrt(i*1.0);j++)
  32. {
  33. if(i%j==0)
  34. {
  35. flag=false;
  36. break;
  37. }
  38. }
  39. if(flag)
  40. {
  41. prime.push(i);
  42. }
  43. }
  44. //cal
  45. long int iter=n;
  46. while(true)
  47. {
  48. int tmp=prime.front();
  49. prime.pop();
  50. int num=0;
  51. while(iter%tmp==0)
  52. {
  53. iter=iter/tmp;
  54. num++;
  55. }
  56. if(num!=0)
  57. col.push_back(make_pair(tmp,num));
  58. if(prime.empty()||iter==1)
  59. break;
  60. }
  61. if(col.size()==0)
  62. {
  63. printf("%ld=%ld\n",n,n);
  64. continue;
  65. }
  66. printf("%ld=",n);
  67. vector<pair<long int,long int> >::iterator it;
  68. int num=0;
  69. for(it=col.begin();it!=col.end();it++)
  70. {
  71. if(it->second==1)
  72. {
  73. printf("%ld",it->first);
  74. }
  75. else
  76. {
  77. printf("%ld^%ld",it->first,it->second);
  78. }
  79. num++;
  80. if(num!=col.size())
  81. printf("*");
  82. else
  83. printf("\n");
  84. }
  85. }
  86. return 0;
  87. }

转载于:https://www.cnblogs.com/championlai/p/4117103.html

发表评论

表情:
评论列表 (有 0 条评论,293人围观)

还没有评论,来说两句吧...

相关阅读