码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5224 解题心得

时间:2015-08-02 19:54:04      阅读:229      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!