当前位置:网站首页>正方形数组的数目(DAY 81)
正方形数组的数目(DAY 81)
2022-07-27 01:06:00 【张学恒】
1:题目
给定一个非负整数数组 A,如果该数组每对相邻元素之和是一个完全平方数,则称这一数组为正方形数组。
返回 A 的正方形排列的数目。
两个排列 A1 和 A2 不同的充要条件是存在某个索引 i,使得 A1[i]≠A2[i]。
输入格式
第一行包含一个整数 n,表示数组 A 的长度。
第二行包含 n 个整数 A[i]。
输出格式
一个整数,表示 A 的正方形排列的数目。
数据范围
1≤n≤12,
0≤A[i]≤109。
输入样例:
3
1 17 8
输出样例:
2
样例解释
[1,8,17] 和 [17,8,1] 都是有效的排列。
2:代码实现
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 15;
int n;
int w[N];
bool st[N];
int ans;
bool check(int x)
{
int r = sqrt(x);
return r * r == x;
}
void dfs(int u, int last)
{
if (u == n) ans ++ ;
else
{
for (int i = 0; i < n; i ++ )
{
if (st[i]) continue; // 被用过了
if (i && !st[i - 1] && w[i] == w[i - 1])
continue;
if (!check(last + w[i])) continue;
st[i] = true;
dfs(u + 1, w[i]);
st[i] = false;
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> w[i];
sort(w, w + n);
for (int i = 0; i < n; i ++ )
if (!i || w[i] != w[i - 1])
{
st[i] = true;
dfs(1, w[i]);
st[i] = false;
}
cout << ans << endl;
return 0;
}
边栏推荐
- On the prototype of constructor
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- [栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
- Baidu cloud face recognition
- 力扣(LeetCode)207. 课程表(2022.07.26)
- [dynamic planning medium] leetcode 198. looting 740. delete and get points
- 【flask】服务端获取客户端的请求头信息
- 商城小程序项目完整源码(微信小程序)
- 一个测试类了解BeanUtils.copyProperties
- Complete source code of mall applet project (wechat applet)
猜你喜欢

易灵思T35 FPGA驱动LVDS显示屏

CAS deployment and successful login jump address

HCIP第十四天笔记

Boom 3D全新2022版音频增强应用程序App

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

IDEA 连接数据库查询数据后控制台表头中文乱码的解决方法

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions

Cloud development sleeping alarm clock wechat applet source code

Complete source code of mall applet project (wechat applet)

Play a parallel multithreaded mcu-mc3172
随机推荐
Redis四大特殊数据类型的学习和理解
阿里云技术专家杨泽强:弹性计算云上可观测能力的构建
易灵思T35 FPGA驱动LVDS显示屏
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
Shortcut keys commonly used in idea
确定了,2022下半年软考报名8月开始
day6
Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
数据库红的表如何设计才能使性能更加优化
Use the most primitive method to manually implement the common 20 array methods
Plato Farm全新玩法,套利ePLATO稳获超高收益
[SQL简单题] LeetCode 627. 变更性别
Boom 3D new 2022 audio enhancement app
1.28亿美元!芬兰量子计算公司IQM获世界基金支持
How big is the bandwidth of the Tiktok server for hundreds of millions of people to brush at the same time?
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
子模块cache缓存失效
Hcip day 14 notes
机器学习【Matplotlib】
杀毒软件 clamav 的安装和使用