B. Phoenix and Puzzle(思维)

使用直角等腰三角形拼正方形的算法解析
这篇博客讨论了一种数学问题,即如何用给定数量的相同直角等腰三角形拼成一个正方形。通过分析样例,指出当小正方形个数为完全平方数时,可以实现拼接。文章提供了C++代码实现,检查输入的三角形数量是否能组成正方形,并给出了样例测试的解决方案。

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Phoenix is playing with a new puzzle, which consists of n identical puzzle pieces. Each puzzle piece is a right isosceles triangle as shown below.
在这里插入图片描述

A puzzle piece
The goal of the puzzle is to create a square using the n pieces. He is allowed to rotate and move the pieces around, but none of them can overlap and all n pieces must be used (of course, the square shouldn’t contain any holes as well). Can he do it?

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains an integer n (1≤n≤109) — the number of puzzle pieces.

Output
For each test case, if Phoenix can create a square with the n puzzle pieces, print YES. Otherwise, print NO.

Example
inputCopy
3
2
4
6
outputCopy
YES
YES
NO
Note
For n=2, Phoenix can create a square like this:

在这里插入图片描述

For n=4, Phoenix can create a square like this:
在这里插入图片描述

For n=6, it is impossible for Phoenix to create a square.

分析:

题意是有n个相同的直角等腰三角形,要求恰好用这n个直角三角形组成一个正方形。其实由样例可以看出1个小正方形最少可以由2个或4个直角三角形组成,而大正方形由x*x个小正方形组成,x是大正方形的边长,所以只要判断小正方形的个数是不是完全平方数即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1<<21;
int a[110];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,x;
		scanf("%d",&n);
		if(n%2==0)
		{
			x=n/2;
			int t=sqrt(x);
			if(t*t==x) 
			{
				printf("YES\n");
				continue;
			}
		}
		if(n%4==0)
		{
			x=n/4;
			int t=sqrt(x);
			if(t*t==x) 
			{
				printf("YES\n");
				continue;
			}
		}
		printf("NO\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值