Poj 1859 the perfect detection ry & poj2526 center of thinking)

Source: Internet
Author: User

Blog original address: http://blog.csdn.net/xuechelingxiao/article/details/40680981


Poj 1859: The Perfect failed


Question:

Given the coordinates of N points, your task is to determine whether there is a center, so that the centers of these points are symmetric. The condition that a vertex set has a central symmetric point is that there is a vertex S. For each vertex P, another vertex Q exists so that P-S = s-Q.


Solution:

Now that we want to find the center point, there must be two symmetric points in the center. Then, after the point set is sorted, we first find the first and last vertices after sorting, find a center, take a point from the front, take a point from the back, and find a center. If the two centers are consistent, you can continue to take them. If they are not consistent, there is no center.


Just look at the code.

#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;struct Point {    double x, y;} P[20010], o;int cmp(Point a, Point b) {    if(a.x == b.x)        return a.y < b.y;    return a.x < b.x;}int main(){    int n;    while(~scanf("%d", &n) && n) {        bool flag = true;        for(int i = 1; i <= n; ++i) {            scanf("%lf%lf", &P[i].x, &P[i].y);        }        sort(P+1, P+n+1, cmp);        o.x = (P[1].x+P[n].x)/2;        o.y = (P[1].y+P[n].y)/2;        for(int i = 2; i <= n; ++i) {            double x = (P[i].x+P[n-i+1].x)/2;            double y = (P[i].y+P[n-i+1].y)/2;            if(x != o.x || y != o.y) {                flag = false;                //printf("%d\n", i);                break;            }        }        if(flag) {            printf("V.I.P. should stay at (%.1lf,%.1lf).\n", o.x, o.y);        }        else {            printf("This is a dangerous situation!\n");        }    }    return 0;}
POJ 2526 :Center of symmetry
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;struct Point {    double x, y;} P[20010], o;int cmp(Point a, Point b) {    if(a.x == b.x)        return a.y < b.y;    return a.x < b.x;}int main(){    int n;    int T;    scanf("%d", &T);    while(T--) {        bool flag = true;        scanf("%d", &n);        for(int i = 1; i <= n; ++i) {            scanf("%lf%lf", &P[i].x, &P[i].y);        }        sort(P+1, P+n+1, cmp);        o.x = (P[1].x+P[n].x)/2;        o.y = (P[1].y+P[n].y)/2;        for(int i = 2; i <= n; ++i) {            double x = (P[i].x+P[n-i+1].x)/2;            double y = (P[i].y+P[n-i+1].y)/2;            if(x != o.x || y != o.y) {                flag = false;                //printf("%d\n", i);                break;            }        }        if(flag) {            printf("yes\n");        }        else {            printf("no\n");        }    }    return 0;}




Poj 1859 the perfect detection ry & poj2526 center of thinking)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.