zoukankan      html  css  js  c++  java
  • hdu 5224 Tom and paper 水题

    Tom and paper

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

    http://acm.uestc.edu.cn/#/contest/show/61

    Description

    Tom面前有一张纸,它的长和宽都是整数。Tom知道这张纸的面积n,他想知道这张纸的周长最小是多少。A的某一段完全重合,或者能够经过上下左右平移与折线A的某一段完全重合,则表示秋实大哥吹出了妹子的一部分旋律。

    Input

    有多组数据。第一行一个正整数T,表示数据组数。接下来T行,每行一个正整数n,表示纸的面积。
    T≤10,n≤109

    Output

    对于每组数据输出一行一个整数,表示答案。

    Sample Input

    3
    2
    7
    12

    Sample Output

    6
    16
    14

    HINT

    题意

    题解:

    枚举这张纸可能的长宽。因为面积为n的矩形必定存在一条边的边长不超过n√,所以只需枚举较短的边长,判断较长的边长是否是整数就可以了。
    因为面积确定的矩形,长宽差越小,周长越小,所以可以从n√开始递减地枚举较短的边长,第一个合法的矩形就是答案。
    时间复杂度:O(n√)

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int main()
    {
        int n;
        int t=read();
        while(t--)
        {
            n=read();
            int ans=0;
            for(int i=sqrt(n+1);i>=1;i--)
            {
                if(i*(n/i)==n)
                {
                    ans=(2*i+2*(n/i));
                    break;
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    贪心算法与动态规划
    Linux重要目录结构
    博客园添加目录索引
    冒泡排序&插入排序&其他排序
    Linux下部署自己写的Web项目
    Java算法入门-数组&链表&队列
    Java集合-数据结构之栈、队列、数组、链表和红黑树
    Java集合-单例模式斗地主&Collections类的shuffle方法了解
    什么是反向代理服务器
    Linux信号处理
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4491198.html
Copyright © 2011-2022 走看看