Problem C HDU 5224
2015-08-02 21:03
302 查看
Description
There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this paper.
Input
In the first line, there is an integer T indicates the number of test cases. In the next T lines, there is only one integer n in every line, indicates the area of paper.
T\leq 10,n\leq {10}^{9}
Output
For each case, output a integer, indicates the answer.
Sample Input
3
2
7
12
Sample Output
6
16
14
题目大意:给出一张纸的面积,要求输出这张纸的最少周长。
思路:纸的面积等于长乘以宽,即一个数等于它的两个质因子数之积,当有多个质因子时,
每两个质因子数之积等于该数的分为一组,分别以这两个质因子作为长宽算出其周长再比较大小,最后输出最小的那个值。
由于时间为1s,给出的n的最大值为1e9,直接写1-n一定会超时,所以先将其开平方就减为1e4-1e5了,这样就不会超时了。
There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this paper.
Input
In the first line, there is an integer T indicates the number of test cases. In the next T lines, there is only one integer n in every line, indicates the area of paper.
T\leq 10,n\leq {10}^{9}
Output
For each case, output a integer, indicates the answer.
Sample Input
3
2
7
12
Sample Output
6
16
14
题目大意:给出一张纸的面积,要求输出这张纸的最少周长。
思路:纸的面积等于长乘以宽,即一个数等于它的两个质因子数之积,当有多个质因子时,
每两个质因子数之积等于该数的分为一组,分别以这两个质因子作为长宽算出其周长再比较大小,最后输出最小的那个值。
由于时间为1s,给出的n的最大值为1e9,直接写1-n一定会超时,所以先将其开平方就减为1e4-1e5了,这样就不会超时了。
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; int main() { int t,s; double m; scanf("%d",&t); while(t--) { int x,y,c; int mini=1e9; scanf("%d",&s); m=(double)sqrt(s); for(x=1;x<=m;x++) { if(s%x==0) { y=s/x; c=(x+y)*2; mini=min(c,mini); } } printf("%d\n",mini); } }
相关文章推荐
- Asp.net-知识总结(3)
- synchronized 与 static synchronized 的区别
- ContentResolver之读取联系人
- ContentReolver之打电话记录
- UnrealEngine4编码风格的思考
- iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
- 面试准备--数据结构与算法(二)--二叉查找树的图文解析及其java实现
- 使用WebRTC搭建前端视频聊天室-01——入门篇
- 【Android多媒体】NuPlayer for HTTP Live Streaming【2】
- 8.2笔记
- 反射invoke方法之我见
- hdu1241(Oil Deposits)
- UVSLive 6324 求射箭覆盖的期望
- 夜梦
- 美轮美奂宇宙星空制作神器Spacescape
- action的结果处理器
- 分布式程序防止多用户对同一数据更新问题
- 在 Linux 下安装基于 Python 的机器学习编程环境
- Problem A CodeForces 556A
- 二项分布 多项分布 伽马函数 Beta分布