1. 程式人生 > >poj1730 - Perfect Pth Powers(完全平方數)(水題)

poj1730 - Perfect Pth Powers(完全平方數)(水題)

ostream splay -- size 技術 () isp close for

/*

以前做的一道水題,再做精度控制又出了錯///。。。

*/

題目大意:

求最大完全平方數,一個數b(不超過int範圍),n=b^p,使得給定n,p最大;

題目給你一個數n,求p ;

解題思路:

不需要遍歷b,只需要從31開始遍歷p就好了。這個方法涉及到我以前過分逃避的精度控制問題:本題會使用函數pow

而pow的返回值是double轉化成int 會有損失,比如4的double表示可以使4.00000000或者3.99999999999;而我們使用

強制類型轉換會截取整數部分結果就可能是3,因此只需要對強制轉換的數據進行+0.1操作,可以解決這問題。

具體做法見代碼。

代碼:

技術分享
#include<iostream>
#include 
<vector> #include <algorithm> #include <queue> #include<set> #include <cstdio> #include<iterator> #include <cmath> using namespace std; int main() { int a; while (cin>>a&&a) { if(a>0) { for (int i=31
; i>=1; i--) { int x=(int )(pow(a*1.0,1.0/i)+0.1); int y=(int )(pow(x*1.0,1.0*i)+0.1); if (y==a) { cout<<i<<endl; break; } } } else { a
=-a; for (int i=31;i>=1;i-=2) { int x=(int )(pow(a*1.0,1.0/i)+0.1); int y=(int )(pow(x*1.0,i*1.0)+0.1); if (y==a) { cout<<i<<endl; break; } } } } }
View Code

ps:這題的n我不能設成long long ,具體原因暫時還不知道,日後更新。

poj1730 - Perfect Pth Powers(完全平方數)(水題)