POJ 1939 Diplomatic License【求线段中点】

                                    POJ 1939 Diplomatic License

                                                     http://poj.org/problem?id=1939

题意

给你n(n>=3且n为奇数)个点的坐标,这n(按构成多边形的顺序输入的)个点必然构成一个多边形,要你按输入顺序输出这个多边形各边的中点坐标。

输入

输入至文件结束。每组先输入一个整数n,表示有n个点,紧接着给出n个点的二维坐标。

输出

每组输入给出n个中点坐标,结果保留6位小数。

样例输入

5 10 2 18 2 22 6 14 18 10 18
3 -4 6 -2 4 -2 6
3 -8 12 4 8 6 12

样例输出

5 14.000000 2.000000 20.000000 4.000000 18.000000 12.000000 12.000000 18.000000 10.000000 10.000000
3 -3.000000 5.000000 -2.000000 5.000000 -3.000000 6.000000
3 -2.000000 10.000000 5.000000 10.000000 -1.000000 12.000000

分析

直接按输入顺序求出中点坐标,然后输出即可,注意最后一个中点是输入的起点和终点的中点,具体看程序。

C++程序

#include<iostream>

using namespace std;

const double EPS=1e-8;

struct Point{
	double x,y;
	Point(){}
};

int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		Point s,p1,p2;
		printf("%d",n);
		scanf("%lf%lf",&s.x,&s.y);
		p1=s;
		for(int i=2;i<=n;i++)
		{
			scanf("%lf%lf",&p2.x,&p2.y);
			printf(" %.6lf %.6lf",(p1.x+p2.x)/2.0,(p1.y+p2.y)/2.0);
			p1=p2;
		}
		printf(" %.6lf %.6lf\n",(s.x+p2.x)/2.0,(s.y+p2.y)/2.0);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/85029073