SRM 668 DIV 2 IsItASquare 600-point
来源:互联网 发布:智能电视专用软件 编辑:程序博客网 时间:2024/05/18 03:22
Problem Statement
It’s a bird! It’s a plane! No, it’s a square in a plane! Wait, is it really a square? There are four distinct points in the plane. You are given their coordinates in the vector s x and y: for each i between 0 and 3, inclusive, there is a point at (x[i], y[i]). Return “It’s a square” (quotes for clarity) if the four points are the vertices of a square. Otherwise, return “Not a square”.
Definition
Class:
IsItASquare
Method:
isSquare
Parameters:
vector , vector
Returns:
string
Method signature:
string isSquare(vector x, vector y)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Constraints
x will contain 4 elements.
y will contain 4 elements.
Each element of x will be between 0 and 10,000, inclusive.
Each element of y will be between 0 and 10,000, inclusive.
The four points described by x and y will be distinct.
Examples
0)
{0, 0, 2, 2}
{0, 2, 0, 2}
Returns: “It’s a square”
1)
{0, 1, 5, 6}
{1, 6, 0, 5}
Returns: “It’s a square”
Note that the sides of the square do not have to be parallel to the coordinate axes. Also note that the order in which the points are given does not have to be the same as the order in which you would encounter them when following the boundary of the square.
2)
{0, 0, 7, 7}
{0, 3, 0, 3}
Returns: “Not a square”
3)
{0, 5000, 5000, 10000}
{5000, 0, 10000, 5000}
Returns: “It’s a square”
4)
{1, 2, 3, 4}
{4, 3, 2, 1}
Returns: “Not a square”
5)
{0, 5, 3, 8}
{0, 0, 4, 4}
Returns: “Not a square”
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
My Solution
#include <iostream>#include <vector>#include <string>using namespace std;class IsItASquare{ public: string isSquare(vector <int> x, vector <int> y); private: //距离平方 double distance(int x1, int y1, int x2, int y2); //对角线的中点是否重合 bool isMidSame(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);};string IsItASquare::isSquare(vector <int> x, vector <int> y){ string ans; if (x.size() != 4 || y.size() != 4) { ans = "Not a square"; return ans; } for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) if (x[i] == x[j] && y[i] == y[j]) { ans = "Not a square"; return ans; } } if (distance(x[0], y[0], x[1], y[1]) == distance(x[2], y[2], x[3], y[3]) && isMidSame(x[0], y[0], x[1], y[1], x[2], y[2], x[3], y[3]) && distance(x[0], y[0], x[2], y[2]) == distance(x[2], y[2], x[1], y[1]) ) ans = "It's a square"; if (distance(x[0], y[0], x[2], y[2]) == distance(x[1], y[1], x[3], y[3]) && isMidSame(x[0], y[0], x[2], y[2], x[1], y[1], x[3], y[3]) && distance(x[0], y[0], x[1], y[1]) == distance(x[1], y[1], x[2], y[2]) ) ans = "It's a square"; if (distance(x[0], y[0], x[3], y[3]) == distance(x[1], y[1], x[2], y[2]) && isMidSame(x[0], y[0], x[3], y[3], x[1], y[1], x[2], y[2]) && distance(x[0], y[0], x[1], y[1]) == distance(x[1], y[1], x[3], y[3]) ) ans = "It's a square"; ans = "Not a square"; return ans;}double IsItASquare::distance(int x1, int y1, int x2, int y2){ return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);}bool IsItASquare::isMidSame(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){ if (x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4) return true; else return false;}
- SRM 668 DIV 2 IsItASquare 600-point
- SRM 668 DIV 2 VerySecureEncryption 250point
- SRM 668 DIV 2 AnArray 1000-point
- SRM 667 DIV 2 PointDistance 250-point
- SRM 669 DIV 2 LiveConcert 250-point
- SRM 669 DIV 2 CombiningSlimes 500-point
- SRM 670 DIV 2 Cdgame 250-point
- TopCoder算法竞赛题2:SRM 146 DIV 2, 250-point
- TopCoder算法竞赛题1:SRM 144 DIV 2, 250-point
- TopCoder算法竞赛题3:SRM 147 DIV 2, 250-point
- TopCoder算法竞赛题4:SRM 148 DIV 2, 250-point
- TopCoder算法竞赛题5:SRM 149 DIV 2, 250-point
- TopCoder算法竞赛题6:SRM 150 DIV 2, 250-point
- Top Coder Arena SRM 144 DIV 2 550point
- SRM 458 Div 2
- SRM 459 DIV 2
- SRM 480 DIV 2
- SRM 527 DIV 2
- 第三周顺序表的应用(1)
- iOS学习笔记29-__block 与 __weak的区别理解
- Linux高可用性方案之Heartbeat安装
- nyoj 68 三点顺序
- VS2015中配置Pthread
- SRM 668 DIV 2 IsItASquare 600-point
- Qt 与 xcode 7版本的问题
- iOS 使用safari
- spring MVC配置详解
- 关于dp,sp,px之间转换的小工具类
- [CodeChef September Challenge 2012]Knight Moving(KNGHTMOV)题解翻译
- 创建表空间
- AD/DNS/DHCP/IIS/WINS的形象定义及关系(转)
- iOS SDK详解之UIEvent/UITouch