HDU 5224 解题心得

原题

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. 
$Tleq 10,nleq {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
原文地址:https://www.cnblogs.com/shawn-ji/p/4696571.html