#NOIP模拟赛#多边形Polygon(枚举 + 模拟)

本文介绍了一种通过预处理和遍历所有可能的点对来寻找满足特定条件的多边形的方法。该方法旨在找到边长差最小的多边形,并通过一系列数学运算确保所求多边形的合法性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >








Code:算法一实现(慢死)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const double eps = 1e-6;

struct node{
    int x, y, dis;
    node(){}
    node(int a, int b, int c){  x = a, y = b, dis = c;}
}P[20500];

int L, cnt;

int abs(int x){ return x < 0 ? -x : x;}

void Pre_Work(){
    int up = L / 2, tmp;
    for(int i = 1; i <= L; ++ i)
        for(int j = 0; i * i + j * j <= up * up; ++ j){
            int d = sqrt(tmp = i * i + j * j);
            if(d * d == tmp)
                P[++ cnt] = node(i, j, d);
        }
}

bool Check(int a, int b){
    return P[a].x * P[b].y - P[a].y * P[b].x;
}

int main(){
    freopen("polygon.in", "r", stdin);
    freopen("polygon.out", "w", stdout);
    scanf("%d", &L);
    if(L & 1 || L == 2)   {printf("-1.000000\n"); return 0;}
    Pre_Work();
    int tmp, d, Ans = 0x3f3f3f3f;
    for(int i = 1; i <= cnt; ++ i)
        for(int j = i + 1; j <= cnt; ++ j){
            d = sqrt(tmp = (P[i].x - P[j].x) * (P[i].x - P[j].x) + (P[i].y - P[j].y) * (P[i].y - P[j].y));
            if(d * d == tmp && Check(i, j) && d + P[i].dis + P[j].dis == L)
                Ans = min(Ans, max(abs(d - P[i].dis), max(abs(P[i].dis - P[j].dis), abs(P[j].dis - d))));
        }
    if(Ans != 0x3f3f3f3f)
        printf("%d.000000\n", Ans);
    else printf("%d.000000\n", (L / 2) & 1);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值