首页 > 其他 > 详细

HDU 5224 解题心得

时间:2015-08-02 19:53:48      阅读:237      评论:0      收藏:0      [点我收藏+]

原题

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
 
 
我的代码:
技术分享
#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>

using namespace std;

int is_prime(int x)
{
    for (int i = 2; i * i <= x; i++)
    if (x % i == 0) return 0;
    return 1;
}
int main()
{
    int t, n;
    int sq;
    cin >> t;
    while (t--)
    {
        int ans,temp;
        int flag = 0;
        cin >> n;
        if (is_prime(n))
            ans = 2 + 2 * n;
        sq = sqrt(n);
        for (int i = sq; i > 0; i--)
        {
            if (n%i == 0)
            {
                temp = n / i;
                ans =2* i +2 * temp;
                break;
            }
        }
        
        cout << ans << endl;
    }


    return 0;
}
View Code

 

HDU 5224 解题心得

原文:http://www.cnblogs.com/shawn-ji/p/4696571.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!