2014-09-13 61 views
0

我正在嘗試爲我的班級編寫代碼。該程序必須讓用戶輸入一個數字,它將輸出每個階乘素因子的指數。例如,當用戶輸入數字5時,輸出將是3 1 1(2^3,3^1,5^1)。到目前爲止,我有代碼來獲得因子的主要因素。但我無法獲得指數。因子的素因子的輸出功率

我的代碼如下:

#include <iostream> 
#include <conio.h> 
#include <windows.h> 
using namespace std; 

int main() 
{ 
int number, factor, exp, product, x, factorial=1; 

cout <<"PRIME FACTORIALS" <<endl; 
cout <<" " <<endl; 
cout <<"Welcome! This program allows users to find the prime factors of a number and its exponents or how many times each prime factor is multiplied." <<endl; 
cout <<" " <<endl; 
cout <<"To begin, please input a positive integer below:" <<endl; 
cin >>number; 

if ((number<1) || (number>100)) 
{ 
    cout <<"You have entered a number that is out of range. Please enter a number from 1-100." <<endl; 
    system("PAUSE"); 
    system("cls"); 
    main(); 
} 

else 
{ 
    for (x=1; x<=number; x++) 
    { 
     factorial=factorial*x; 

     for (factor=2; factor<=factorial; factor++) 
     { 
      while (factorial%factor==0) 
      { 
       factorial/=factor; 
       cout <<factor <<" "; 
      } 
     } 
    } 
} 
} 
+0

是的,我修復了這個問題。任何想法如何我可以輸出主要因素的指數? – Jean 2014-09-13 15:35:37

+0

計算每個因子的分割成功次數。 (儘管你目前正在嘗試所有可能的因素,而不僅僅是主要因素。) – 2014-09-13 15:57:06

+0

https://stackoverflow.com/questions/21196814/prime-numbers-and-factorials/21235844#21235844 – 2016-02-18 13:18:43

回答

0

你不應該完成的工作計算出它的因素之前的階乘?即在factorial = factorial * x;之後有}