hdu6154 CaoHaha's staff CCPC网赛1005 找规律+构造

http://acm.split.hdu.edu.cn/showproblem.php?pid=6154

题意:在坐标系中,你可以连长度为sqrt(2)的对角线或者就是单位长度的边,要求最少的边构成面积为n的图形。求这个最小边。

题解:可以发现如果边数是4的倍数,构造菱形是面积最大的。那在这个基础上加一条边,两条边,三条边,四条边呢?

下面四幅图len为边长,另一个数字是增加的面积(画的有些残。。。):




代码:

#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define d(a) cout<<a<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;

const int N = 100000 + 5;
const int mod = 1000000000 + 7;
const double eps = 1e-8;

int main(){
	int t,len,area,n;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		if(n==1||n==2){
			puts("4");
		}
		else if(n==3||n==4){
			puts("6");
		}
		else if(n==5){
			puts("7");
		}
		else if(n==6||n==7||n==8){
			puts("8");
		}
		else{
			//向下取整,len为构成菱形,在菱形的一条边要画的数量 
			len=floor(sqrt(n/2));
			area=len*len*2;
			if(n==area){
				printf("%d\n",len*4);
			} 
			else if(n<=area+len-0.5){//加一条边 
				printf("%d\n",len*4+1); 
			}
			else if(n<=area+len*2){//加两条边 
				printf("%d\n",len*4+2);
			}
			else if(n<=area+len*3+0.5){//加三条边 
				printf("%d\n",len*4+3);
			}
			else{//加四条边 
				printf("%d\n",len*4+4);
			} 
		}
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值