題目如下
給出一個數N,問N是否能表示成某個正整數X的K次冪(K>1),N可能有多種表示方法,請找出最大的X並輸出相應的K
一點小問題:提交了三遍原來是編譯器的問題,無pow(int&, int)的重載,故出現call of overloaded `pow(int&, int)’ ,只能最多指數是整型,解決方法,對底數(地鼠)使用強制轉換成double
#include <iostream>
#include <string>
#include<cmath>
using namespace std;
int main()
{
int a, b;
while (cin >> a&&a != 0)
{
b = a/2; //不能用sqrt提升所謂的性能,因爲要截斷,
//如8開根號2點多
if (pow((double)b, 2) == a)
{
cout << b << " " << 2 << endl;
}
else
{
b = b - 1;
while (b>1)
{
int i = 0;
for (; pow((double)b, (double)i)<a; i++); //有些編譯器嚴格要求pow用double類型的,16.7.1
if (pow((double)b, (double)i) == a)
{
cout << b << " " << i << endl;
break;
}
b--;
}
if (b == 1)
{
cout << 0 << " " << 0 << endl;
}
}
}
return 0;
}