终于ac一道题了...
考完试以后发现自己的智商下降了不少,加油啊,suikay!!
不过这个久违的ac是抄来的...
觉得这道题很多东西要学,就把自己觉得有用的加个注释~不过这道题也有点太变态了..
以前一直以为只有double转int会丢信息,原来int转double也会丢...
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <iostream>
- using namespace std;
- int main()
- {
- long x,y;
- //注意了这里n一定要用double,如果用long会wa
- double n,b,up,down;
- while(cin >> n && n)
- {
- if( n > 0 )
- {
- for(int i = 32; i > 0; i--)
- {
- //使用pow函数直接将n开i次方
- b = pow(n,1.0/i);
- x = b;
- y = x + 1;
- // 检查b是否为整数
- if((b - x <= 1e-12) || ( y - b <= 1e-12)) {
- cout << i << endl;
- break;
- }
- }
- }
- else
- {
- n = -n;
- //如果n是负数,那么指数只能是奇数了
- for(int i = 31; i >= 1;i-=2)
- {
- b = pow(n,1.0/i);
- x = b;
- y = x + 1;
- if((b - x <= 1e-12 ) || (y - b <= 1e-12)) {
- cout << i << endl;
- break;
- }
- }
- }
- }
- return 0;
- }