CaoHaha's staff(找规律)

本文介绍了一种算法,用于解决使用最少数目的线段构造一个多边形的问题,确保其面积不小于给定值。线段长度限定为1或根号2。通过构造菱形并逐步扩展的方法,有效地解决了该问题。

Problem Description
"You shall not pass!"
After shouted out that,the Force Staff appered in CaoHaha's hand.
As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.
But now,his new owner,CaoHaha,is a sorcerers apprentice.He can only use that staff to send things to other place.
Today,Dreamwyy come to CaoHaha.Requesting him send a toy to his new girl friend.It was so far that Dreamwyy can only resort to CaoHaha.
The first step to send something is draw a Magic array on a Magic place.The magic place looks like a coordinate system,and each time you can draw a segments either on cell sides or on cell diagonals.In additional,you need 1 minutes to draw a segments.
If you want to send something ,you need to draw a Magic array which is not smaller than the that.You can make it any deformation,so what really matters is the size of the object.
CaoHaha want to help dreamwyy but his time is valuable(to learn to be just like you),so he want to draw least segments.However,because of his bad math,he needs your help.

Input
The first line contains one integer T(T<=300).The number of toys.
Then T lines each contains one intetger S.The size of the toy(N<=1e9).

Output
Out put T integer in each line ,the least time CaoHaha can send the toy.
 
Sample Input
5 1 2 3 4 5

Sample Output
4 4 6 6 7

题意:给你n个面积,让你用最少的线构造出多边形使得面积不小于给定的值,你只能画长度为一或根号二的线。

思路:先构造菱形,每次在菱形的基础上扩展,使其总边数加一,方法参考自:

https://blog.youkuaiyun.com/LSC_333/article/details/77428687

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int x;
int main()
{
	int t;
	cin>>t;
	for(int i=0;i<t;i++)
    {
    	cin>>x;
    	if(x==1)   //面积为一的情况特判
    	   cout<<"4"<<endl;
    	else
    	{
    		int n=sqrt(x/2);   //求出基础菱形的边长
	    	if(2*n*n>=x)
	    	   cout<<4*n<<endl;
	    	else if(2*n*n+(2*n-1)/2>=x)
	    	   cout<<4*n+1<<endl;
	    	else if(2*n*n+2*n>=x)
	    	   cout<<4*n+2<<endl;
	    	else if(2*n*n+3*n+1/2>=x)
	    	   cout<<4*n+3<<endl;
	    	else
	    	   cout<<4*(n+1)<<endl;
	    	}
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值