Area

来源:互联网 发布:淘宝上的减肥药可信吗 编辑:程序博客网 时间:2024/04/30 09:40

Area

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2
Problem 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
4582567256244865
 

Sample Output
000.52
 

Source
PKU
 
结果要用__int64 存储,判断res 是否为奇数,是奇数要加.5;
代码如下:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct point{    int x , y ;};int dx[10]={0,-1,0,1,-1,0,1,-1,0,1};int dy[10]={0,-1,-1,-1,0,0,0,1,1,1};point data[1000005];int cnt;char str[1000005];int inter(int i , int j){    return data[i].x*data[j].y - data[j].x*data[i].y;}int main(){    int t ;    scanf("%d",&t);    data[0].x = 0;    data[0].y = 0;    while(t --)    {        scanf("%s",str);        cnt = 0;        for(int i = 0; str[i]!= '\0'; i ++)        {            if(str[i] == '5')break;            cnt = i+1;            data[i+1].x = data[i].x+dx[str[i]-'0'];            data[i+1].y = data[i].y+dy[str[i]-'0'];        }        __int64 res = 0;        for(int i = 1; i < cnt; i ++)        {            res += inter(i, i+1);        }        if(res < 0) res = -res;        if(res % 2)            printf("%I64d.5\n",res/2);        else            printf("%I64d\n",res/2);    }    return 0;}


0 0
原创粉丝点击