POJ 1654 Area

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 19635 Accepted: 5375

Description

You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From this vertex, you may go step by step to the following vertexes of the polygon until back to the initial vertex. For each step you may go North, West, South or East with step length of 1 unit, or go Northwest, Northeast, Southwest or Southeast with step length of square root of 2. 

For example, this is a legal polygon to be computed and its area is 2.5: 

Input

The first line of input is an integer t (1 <= t <= 20), the number of the test polygons. Each of the following lines contains a string composed of digits 1-9 describing how the polygon is formed by walking from the origin. Here 8, 2, 6 and 4 represent North, South, East and West, while 9, 7, 3 and 1 denote Northeast, Northwest, Southeast and Southwest respectively. Number 5 only appears at the end of the sequence indicating the stop of walking. You may assume that the input polygon is valid which means that the endpoint is always the start point and the sides of the polygon are not cross to each other.Each line may contain up to 1000000 digits.

Output

For each polygon, print its area on a single line.

Sample Input

4
5
825
6725
6244865

Sample Output

0
0
0.5
2

 

#include <stdio.h>
#include <string.h>
#include <math.h>

int dx[10]={0,1,1,1,0,0,0,-1,-1,-1};
int dy[10]={0,-1,0,1,-1,0,1,-1,0,1};
char s[1000010];
int area,x,y,px,py;

int main(){
    int t,tmp,i;
    scanf("%d",&tmp);
    while(tmp--){
        scanf("%s",s);
        t=(int)strlen(s);
        if(t<3) {printf("0\n");continue;}
        area=0;
        x=y=0;
        for(i=0;i<t-1;i++){
            px=x+dx[s[i]-'0'];
            py=y+dy[s[i]-'0'];
            area+=(px*y-x*py);//这个是看了别人的算法之后才写的,我还是没想明白为什么可以这样表示面积
            x=px;
            y=py;
        }
        if(area<0) area=(-1)*area;
        if(area%2==0)
            printf("%d\n",area/2);
        else printf("%d.5\n",area/2);
    }
    return 0; 
}

  

源码地址: https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
《基于STM32微控制器集成温湿度监测与显示功能的系统实现方案》 本方案提供了一套完整的嵌入式系统设计参考,实现了环境参数的实时采集、可视化呈现与异常状态提示。系统核心采用意法半导体公司生产的STM32系列32位微控制器作为主控单元,负责协调各外设模块的工作流程。 系统通过数字式温湿度复合传感器周期性获取环境参数,该传感器采用单总线通信协议,具有响应迅速、数据可靠的特点。采集到的数值信息通过两种途径进行处理:首先,数据被传输至有机发光二极管显示屏进行实时图形化显示,该显示屏支持高对比度输出,能够在不同光照条件下清晰呈现当前温度与湿度数值;其次,所有采集数据同时通过通用异步收发传输接口输出,可供上位机软件进行记录与分析。 当监测参数超出预设安全范围时,系统会启动声学警示装置,该装置可发出不同频率的提示音,以区分温度异常或湿度异常状态。所有功能模块的驱动代码均采用模块化设计原则编写,包含完整的硬件抽象层接口定义、传感器数据解析算法、显示缓冲区管理机制以及串口通信协议实现。 本参考实现重点阐述了多外设协同工作的时序控制策略、低功耗数据采集模式的应用方法,以及确保系统稳定性的错误处理机制。代码库中包含了详细的初始化配置流程、中断服务程序设计和各功能模块的应用程序接口说明,为嵌入式环境监测系统的开发提供了可靠的技术实现范例。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值