poj1730 - Perfect Pth Powers(完全平方数)(水题)
/*
以前做的一道水题,再做精度控制又出了错///。。。
*/
题目大意:
求最大完全平方数,一个数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>)
{
for (int i=; i>=; 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=;i>=;i-=)
{
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;
}
}
}
}
}
ps:这题的n我不能设成long long ,具体原因暂时还不知道,日后更新。
poj1730 - Perfect Pth Powers(完全平方数)(水题)的更多相关文章
- [暑假集训--数论]poj1730 Perfect Pth Powers
We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- POJ 1730 Perfect Pth Powers(暴力枚举)
题目链接: https://cn.vjudge.net/problem/POJ-1730 题目描述: We say that x is a perfect square if, for some in ...
- Kattis之旅——Perfect Pth Powers
We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, ...
- poj 1730 Perfect Pth Powers
这个有2种方法. 一种是通过枚举p的值(p的范围是从1-32),这样不会超时,再就是注意下精度用1e-8就可以了,还有要注意负数的处理…… #include<iostream> #incl ...
- UVa 10622 (gcd 分解质因数) Perfect P-th Powers
题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...
- uva10622 Perfect P-th Powers
留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...
- UVA 10622 Perfect P-th Powers
https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...
随机推荐
- python学习笔记2-tuple
tuple: #元组也是List,但是值不能变 a=(') print(a[1]) mysql=(',''root','123456') print(mysql.count('root')) #例子 ...
- cmmusic:小巧而实用的mplayer音乐播放前端
Author: Jiqing (jiqingwu@gmail.com) home: http://hi.baidu.com/jiqing0925 create: 2011-03-10 update: ...
- 基本控件文档-UIView属性
CHENYILONG Blog 基本控件文档-UIView属性 Fullscreen UIView属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...
- Jenkins02:Jenkins+maven+svn集成
1.安装Maven并配置环境变量 下载maven(windows下载zip包,linux下载tar.gz包),然后配置环境变量 在项目中使用maven,可以从java中央仓库中获取到项目所依赖的jar ...
- 20145202马超 2016-2017-2 《Java程序设计》第三次实验
实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 在IDEA中使用工具(Code->Re ...
- Linux 内核驱动--多点触摸接口【转】
转自:http://blog.csdn.net/joard_yang/article/details/6225937 译自:linux-2.6.31.14/Documentation/input/mu ...
- MySQL 四种链接
1.内联接 INNER JOIN(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行. ...
- 三、springboot热部署
1.spring-boot-devtools 实现热部署 spring-boot-devtools 最重要的功能就是热部署.它会监听 classpath 下的文件变动,并且会立即重启应用. <d ...
- RabbitMQ--Publish/Subscribe(五)
上篇文章中,我们实现了不同consumer接收不同级别的日志,这篇文章中,不以日志级别,使用不同日志来源.比如kernel.*.*.critical. 这就要使用topic exchange完成了.将 ...
- 26 About the go command go命令行
About the go command go命令行 Motivation Configuration versus convention Go's conventions Getting star ...