CaoHaha's staff(中国大学生程序选拔赛2017年网络大赛)

来源:互联网 发布:vue服务端渲染 php 编辑:程序博客网 时间:2024/05/16 06:27
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
512345
 

Sample Output
4466

7

分析:

纯粹找规律,从3个格子开始(前两个输出4),四个边一组,比如五条边为零(因为2是四条边,三六条边,五没有),然后六为2,七为1,八为3;之后四组则在原基础上加一。如下

5~89~1213~16012234123345

#include<bits/stdc++.h>using namespace std;int a[4];int main(){     int t;     cin>>t;     while(t--)     {         int n;         cin>>n;         a[0]=0,a[1]=2,a[2]=1,a[3]=3;         int sum=2,f=4;         if(n<2) {cout<<4<<endl;continue;}         else         {             while(sum<n)             {                 for(int i=0;i<4;i++)                 {                    sum+=a[i];                     a[i]++;f++;                     if(sum>=n) {break;}                 }             }             cout<<f<<endl;         }     }}