題目大意:已知機器人行走步數及每一步的座標變化量,求機器人所走路徑圍成的多邊形的面積、多邊形邊上和內部的點的數量。ios
思路:叉積求面積,pick定理求點。spa
pick定理:面積=內部點數+邊上點數/2-1。code
// Time 0ms; Memory 236K
#include<iostream> #include<cstdio> #include<cmath> using namespace std; struct point { int x,y; point(int xx=0,int yy=0):x(xx),y(yy){} }a,b; int gcd(int x,int y) { static int t; for(;t=y;y=x%y,x=t); return x; } int main() { int i,j,t,m,p,q,A,I,E; cin>>t; for(i=0;i<t;i++) { A=E=0; cin>>m; for(j=0;j<m;j++) { cin>>p>>q; a=b; b=point(a.x+p,a.y+q); A+=a.x*b.y-a.y*b.x; E+=abs(gcd(a.x-b.x,a.y-b.y)); } A=abs(A); I=(A-E)/2+1; //pick定理 printf("Scenario #%d:\n",i+1); printf("%d %d %.1lf\n\n",I,E,A*0.5); } return 0; }