ACM已知面积求最小周长

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

解题思路:

题目的大意就是给定一个矩形的面积要我们求出这个矩形的最小周长。

我们知道一个矩形的最大周长是这样产生的,就是当它的一条边等于面积的大小时,另一条边是1.题目中要求长和宽都要是整数,所以我们要求的就是面积的因子的乘机的最小值。

程序代码:

#include <iostream>
using namespace std;
int main()
{
    int T,j;
    int x;
    cin>>T;
    while(T--)
    {
        cin>>x;
        int min=(1+x)*2,t;
        for(int k=2;k*k<=x;k++)
        {
            if(x%k==0)
            {
                j=x/k;
                  t=2*j+2*k;
                if(t<min)
                 min=t;
            }

        }
        cout<<min<<endl;
    }
    return 0;
}
时间: 2024-08-06 16:06:03

ACM已知面积求最小周长的相关文章

已知矩形面积求最小周长

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 te

已知面积求周长

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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 pap

UVA 12386 Smallest Polygon n个点的任意多边形求最小周长 科学的暴力

题目链接: 题意: 给定n个点,用n个点组成的多边形中(可以是凹多边形,但n个点一定要全在多边形上) 在所有能由n个点构成的多边形中 求最小面积的多边形的周长 - 最小周长. 思路: 首先我们选择一个定点,则接下来的数进行一个排列,有(n-1)!个排列. 这个序列相邻两个数之间有一条线段. 判断多边形合法:任意两条线段不相交即可.n^2 剩下就是简单的更新答案了. 所以复杂度是 ( n-1 ) ! * n*n #include <cstdio> #include <cstring>

先序,中序,后序,已知两者求第三者

今天来总结下二叉树前序.中序.后序遍历相互求法,即如果知道两个的遍历,如何求第三种遍历方法,比较笨的方法是画出来二叉树,然后根据各种遍历不同的特性来求,也可以编程求出,下面我们分别说明. 首先,我们看看前序.中序.后序遍历的特性: 前序遍历:     1.访问根节点     2.前序遍历左子树     3.前序遍历右子树 中序遍历:     1.中序遍历左子树     2.访问根节点     3.中序遍历右子树 后序遍历:     1.后序遍历左子树     2.后序遍历右子树     3.访问

已知点和线, 求垂足

已知点 \(A(x_1, y_1)\), 现有一条直线 \(l\): \(ax+by+c=0\), 直线 \(l\) 到\(A\) 的举例为 \(d\), 点 \(A\) 到直线 \(l\) 的垂足点记为 \(B\), 求 \(B\) 的坐标. 解答 已知直线的表达式, 可知直线上的一个垂线量为: \((a,b)\), 记 \(B\) 的坐标表示为 \((x_2,y_2)\), 那么向量 \(\overrightarrow{BA} = (x_1 - x_2, y_1 - y_2)\), \(d(

使用python已知平均数求随机数

问题描述:产生40个数,范围是363-429之间,平均值为402 思路: 1 产生一个随机数 2 使用平均数求和随机数求出第二个数,生成20组 3 将排序打乱 # -*- coding: cp936 -*- import random import string ###################产生随机整数################### ###################第一个数随机产生,第二个使用平均数求出################### #count 数字的个数 #a

睡觉前请关灯的 破解尝试版本 由已知解求一个矩阵的步骤

#include<iostream> #include"wz.h" #include<ctime> using namespace std; #define  MAX  5 void show(int arr[][MAX]) {  for(int i=0;i<MAX;i++)  {   for(int j=0;j<MAX;j++)   {    cout<<arr[i][j]<< " ";   }   co

Codeforce 459A - Pashmak and Garden (已知两点求另外两点构成正方形)

Pashmak has fallen in love with an attractive girl called Parmida since one year ago... Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden

一直两个坐标点求旋转角度,或已知角度求移动的方向和位置

namespace Pioneer { export class Game extends Behaviour{ private nState:number; private Camera:Camera2D; nScore:number; nGold:number; private nRotationAngle:number = 0; private nRotationCount:number = 0; private nFrameRateTimer:number; private nScore