Forsaken喜欢正方形(几何思路+模拟)

题目:

大二上----周训A题

思路:算出四个点两两之间的距离,然后按照距离长短sort一下,如果dis[0]==dis[3](长宽相等) &&dis[4]==dis[5](对角线相等)则为正方形

注意,仅长宽相等有可能为菱形,因此仍要判断对角线是否相等。

ACcode:

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int x[4],y[4];
int xa[]={1,-1,0,0};
int ya[]={0,0,1,-1};
int check()
{
	vector<int> dis;
	for(int i=0;i<4;i++)
	{
		for(int j=i+1;j<4;j++)
		{
			int t=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
			dis.push_back(t);
		}
	}
	sort(dis.begin(),dis.end());
	if(dis[0]==dis[3]&&dis[4]==dis[5])
	{
		return 1;
	}
	else
	{
		return 0;
	}
	
}
int main()
{
	for(int i=0;i<4;i++)
	{
		cin >> x[i] >> y[i];
	}
	if(check())
	{
		cout << "wen" << endl;
		return 0;
	}
	else
	{
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				x[i]+=xa[j];
				y[i]+=ya[j];
				if(check())
				{
					cout << "hai xing" << endl;
					return 0;
				}
				x[i]-=xa[j];
				y[i]-=ya[j];
			}
		}
	}
	cout << "wo jue de bu xing" << endl;
	return 0;
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以看出Forsaken喜欢数论,并且在数论方面有一定的研究。以下是一些与数论相关的Forsaken的例子: 1. 线性筛法求质数 ```python def get_primes(n): is_prime = [True] * (n + 1) primes = [] for i in range(2, n + 1): if is_prime[i]: primes.append(i) for j in range(len(primes)): if i * primes[j] > n: break is_prime[i * primes[j]] = False if i % primes[j] == 0: break return primes ``` 2. 分组算法 ```python def group_students(students, k): groups = [[] for _ in range(k)] for i in range(len(students)): groups[i % k].append(students[i]) return groups ``` 3. 正方形计数 ```python def count_squares(n): count = 0 for i in range(1, int(n ** 0.5) + 1): count += (n // i - i + 1) return count ``` 4. 三维数点 ```python def count_3d_points(points): bit = [0] * (max(points, key=lambda x: max(x)) + 1) count = 0 for i in range(len(points)): x, y, z = points[i] count += i - sum(bit[j] for j in range(x)) for j in range(x, len(bit)): bit[j] += 1 return count ``` 5. 独一无二的最小生成树 ```python def unique_minimum_spanning_tree(n, edges): edges.sort(key=lambda x: x[2]) parent = list(range(n)) rank = [0] * n def find(x): if parent[x] != x: parent[x] = find(parent[x]) return parent[x] def union(x, y): px, py = find(x), find(y) if px == py: return False if rank[px] < rank[py]: parent[px] = py elif rank[px] > rank[py]: parent[py] = px else: parent[px] = py rank[py] += 1 return True mst = [] for u, v, w in edges: if union(u, v): mst.append((u, v, w)) return mst --相关问题--:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值