uva 1595 Symmetry“结构体”
给出平面上N(N<=1000)个点。问是否可以找到一条竖线,使得所有点左右对称,如图所示:
3
5
-2 5
0 0
6 5
4 0
2 3
4
2 3
0 4
4 0
0 0
4
5 14
6 10
5 10
6 14
Sample Output
YES
NO
YES 写题永远都是wrong answer.....
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std; const int maxn = ; struct point{
double x, y; bool operator < (const point & rhs) const{
return x < rhs.x;
}
}p[maxn]; int main()
{
int T;
cin >> T;
while(T--){
int n;
double line; //line 为对称轴的位置
bool flag = true; cin >> n; for(int i = ;i < n; i++){
double x, y;
cin >> x >> y;
p[i].x = x;
p[i].y = y;
} sort(p,p+n); for(int i = ; i < n/; i++){
int k = ;
for(int j = n-; j>n/-;j--){
cout<<"** "<<j<<" "<<p[i].y<<" "<<p[j].y<<endl;
if(p[i].y == p[j].y){
p[j].y = -0.111;
k = ;
if(i == )line = ( p[i].x + p[n--i].x ) / 2.0;
else {
double l = ( p[i].x + p[j].x ) / 2.0;
cout<< p[i].x<<" - "<<p[j].x<<endl;
if(l != line){
cout << <<" "<< l<< " "<< line<<endl;
flag = false;
break;
}
}
cout<<"line "<<line<<endl;
break;
}
} if(k){
if(!flag)break;
}
else {
flag = false;
break;
} } if(flag && (n%)){
if(p[n/].x != line )flag = false;
cout<<<<" "<< p[n/].x<<" "<<line<<endl;
} if(flag) cout << "YES" << endl;
else cout << "NO" << endl; }
system("pause");
return ;
}
那就找错。。。
以上代码没有考虑到对称轴上有去多点的情况
将点排序后,先分偶数奇数个点两种情况,根据最中间的两个点或一个点计算出对称轴再进一步判断其他点是否符合
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ; struct point{
double x, y; bool operator < (const point & rhs) const{
return x < rhs.x;
}
}p[maxn]; int main()
{
int T;
cin >> T;
while(T--){
int n ,n0 ;
double line; //line 为对称轴的位置
bool flag = true; cin >> n; for(int i = ;i < n; i++){
double x, y;
cin >> x >> y;
p[i].x = x;
p[i].y = y;
} sort(p,p+n); if(n%){ //奇数个点
line = p[n/].x;
n0 = n/;
}
else { //偶数个点
line = (p[n/].x+p[n/-].x)/2.0;
n0 = n/-;
} for(int i = ; i <= n0; i++){
int k = ;
for(int j = n-; j>n/-;j--){
if(p[i].y == p[j].y){
p[j].y = -0.111;
k = ;
double l = ( p[i].x + p[j].x ) / 2.0; if(l != line){
flag = false;
break;
} break;
}
else{
if(p[i].x == line&&p[n-i-].x == line){
k = ;
break;
}
} } if(k){
if(!flag)break;
}
else {
flag = false;
break;
} } if(flag) cout << "YES" << endl;
else cout << "NO" << endl; }
//system("pause");
return ;
}
uva 1595 Symmetry“结构体”的更多相关文章
- uva 1595 - Symmetry
思路:首先,如果这些点对称,那么它们的对称轴是x = m(m是所有点横坐标的平均值): 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这个点是否在集合里面. 如果有 ...
- UVa 1595 Symmetry(set)
We call a figure made of points is left-right symmetric as it is possible to fold the sheet of paper ...
- UVa 1595 Symmetry (set && math)
题意:给出n个在直角坐标系上的点,问你能不能找出一条竖轴(即垂直于x的轴)使得所有的点根据这条轴对称,能则输出YES,否则输出NO 分析:首先需要找到对称轴的值,将所有n个点的x轴的值加起来然后去除以 ...
- Go结构体实现类似成员函数机制
Go语言结构体成员能否是函数,从而实现类似类的成员函数的机制呢?答案是肯定的. package main import "fmt" type stru struct { testf ...
- C#基础回顾(二)—页面值传递、重载与重写、类与结构体、装箱与拆箱
一.前言 -孤独的路上有梦想作伴,乘风破浪- 二.页面值传递 (1)C#各页面之间可以进行数据的交换和传递,页面之间可根据获取的数据,进行各自的操作(跳转.计算等操作).为了实现多种方式的数据传递,C ...
- go语言结构体
定义: 是一种聚合的数据类型,是由零个或多个任意类型的值聚合成的实体. 成员: 每个值称为结构体的成员. 示例: 用结构体的经典案例处理公司的员工信息,每个员工信息包含一个唯一的员工编号.员工的名字. ...
- C语言中的结构体
用户自己建立自己的结构体类型 1. 定义和使用结构体变量 (1).结构体的定义 C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,它称为结构体. (2).声明一个结构体类型的一般形式为: ...
- C++_系列自学课程_第_12_课_结构体
#include <iostream> #include <string> using namespace std; struct CDAccount { double bal ...
- java socket传送一个结构体给用C++编写的服务器解析的问题
另一端是Java写客户端程序,两者之间需要通信.c++/c接收和发送的都是结构体,而Java是直接发送的字节流或者byte 数组.解决方法:c++/c socket 在发送结构体的时候其实发送的也是字 ...
随机推荐
- Java中的Clone机制(浅层复制)
浅层复制代码: import java.util.*; class Int{ private int i; public Int(int ii){i = ii;} public void increm ...
- No1_5.字符串的基本操作_Java学习笔记
import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; import java. ...
- [转]JS基础之undefined与null的区别
在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...
- php的一些小笔记--时间函数
strtotime 返回UNIX时间戳 .这个函数可以用来计算前天,昨天,后天,明天 例如明天:date('Y-m-d H:is',strtotime('+1 day')) day>1是复数 ...
- 有关service
在symfony2 服务注入容器的时候参数语法可以是这样: cellcom.twig.menu_extension: class: Cellcom\WebBundle\Twig\Extension\M ...
- Network Attack
Network Attack Nicola regularly inspects the local networks for security issues. He uses a smart and ...
- spring bean初始化和销毁
spring bean的创建与消亡由spring容器进行管理,除了使用<bean><property/></bean>进行简单的属性配置之外,spring支持更人性 ...
- 3Sum Smaller 解答
Question Given an array of n integers nums and a target, find the number of index triplets i, j, k w ...
- Shell下通过echo+telnet在远端执行命令
创建脚本cmd.sh,用于输入telnet的用户与密码,以及生成远端需要执行的命令 执行命令 MY_SIGN=/tmp/sign; (sh cmd.sh ) | (telnet localhost ...
- 面试时如何优雅的谈论OC
在面试中,我们经常会遇到一些原理性的问题,很常识但很难用通俗的语言解释清楚,这也是大部分业务级程序员经常失误的地方.虽然写了多年代码,但是核心思想不清,导致自己的后续发展受限,这是一个优秀的程序员和普 ...