我正在尝试编写一个程序,找到数字的素因子并打印它们
DRIVER=SQL Server;SERVER=SERVER\SQLEXPRESS;Integrated Security=SSPI ;DATABASE=<Database name>
控制台输出示例:
请输入正整数:2700
2 2 3 3 3 5 5
我想获得输出的指数表示法,例如:2 ^ 2 x 3 ^ 3 x 5 ^ 2
任何想法我怎样才能做到这一点?感谢
答案 0 :(得分:0)
试试这个
void primeFactors(int num)
{
int fac = 2;
while (num > 1)
{
if (num % fac == 0)
{
cout << fac << "^"; //print the base first
num /= fac;
int pow = 1;
while (num % fac == 0) //get the power of current base
{
num /= fac;
pow++;
}
cout << pow; //print out the power, now we have fac^pow printed
//if not the last factor, print a multiplication symbol
if (num != 1)
cout << " x ";
}
else
{
fac++;
}
}
}