当前位置:   article > 正文

合数分解为质数_c++合数拆分

c++合数拆分

                                                                              

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. /*
  4. 任何一个大于1的非素数整数(即合数)
  5. 都可以唯一分解成若干个素数的乘积
  6. */
  7. int main(){
  8. int n;
  9. while(cin>>n){
  10. if(n==1){
  11. cout<<"error";
  12. }else{
  13. int t=n;
  14. cout<<t<<"=";
  15. for(int i=2;i<n;i++){//n成为新的被分解数
  16. while(n!=i){
  17. if(n%i==0){
  18. cout<<i<<"*";
  19. n=n/i;
  20. }else{
  21. break;
  22. }
  23. }
  24. }
  25. cout<<n<<endl;
  26. }
  27. }
  28. return 0;
  29. }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/393037
推荐阅读
相关标签
  

闽ICP备14008679号