当前位置:网站首页>力扣练习——39 正方形数组的数目
力扣练习——39 正方形数组的数目
2022-08-02 04:18:00 【qq_43403657】
39 正方形数组的数目
1.问题描述
给定一个非负整数数组 A,如果该数组每对相邻元素之和是一个完全平方数,则称这一数组为正方形数组。
返回 A 的正方形排列的数目。两个排列 A1 和 A2 不同的充要条件是存在某个索引 i,使得 A1[i] != A2[i]。
示例 1:
输入:[1,17,8]
输出:2
解释:
[1,8,17] 和 [17,8,1] 都是有效的排列。
示例 2:
输入:[2,2,2]
输出:1
说明:若一个数能表示成某个整数的平方的形式,则称这个数为完全平方数。完全平方数是非负数。
2.输入说明
首先输入A数组的长度n,
然后输入n个整数,以空格分隔。
1 <= n<= 12
0 <= A[i] <= 1e9
3.输出说明
输出一个整数
4.范例
输入
3
1 17 8
输出
2
5.代码
#include <iostream>
#include <queue>
#include <cstdlib>
#include <string>
#include<set>
#include<algorithm>
using namespace std;
int ans;//结果
vector<int>t;
int used[12] = {
0 };
void dfs(vector<int> &nums);
int numSquarefulPerms(vector<int>& nums)
{
sort(nums.begin(), nums.end());//从小到大排序
dfs(nums);
return ans;
}
void dfs(vector<int>& nums) {
if (t.size() == nums.size()) {
ans++;
return;
}
for (int i = 0; i < nums.size(); i++) {
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])continue;
if (!used[i]) {
if (t.size() == 0 || sqrt(nums[i] + t[t.size() - 1]) - int(sqrt(nums[i] + t[t.size() - 1])) == 0) {
used[i] = 1;
t.push_back(nums[i]);//弹入
dfs(nums);
used[i] = 0;//回溯
t.pop_back();//弹出
}
}
}
}
int main()
{
vector<int>nums;
int n,tmp;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> tmp;
nums.push_back(tmp);
}
int res = numSquarefulPerms(nums);
cout << res << endl;
return 0;
}
边栏推荐
- PDF文件转换格式
- MapFi paper structure organization
- CaDDN paper reading of monocular 3D target detection
- 如何评价最近爆红的FastAPI?
- Arduino框架下STM32F1/F4系列HID模式程序烧录教程
- ADSP21489数据手册表摘要
- 日本痴汉打赏女主播1.5亿,结果。。。
- 已更新 联通 电信 tiny模式
- Andrew Ng's Machine Learning Series Course Notes - Chapter 18: Application Example: Image Text Recognition (Application Example: Photo OCR)
- 其他重要协议(DNS,ICMP,NAT,交换机)
猜你喜欢
随机推荐
Deep Blue Academy-Visual SLAM Lecture 14-Chapter 6 Homework
【STM32】 ADC模数转换
Deep blue college - handwritten VIO operations - the first chapter
Pycharm platform import scikit-learn
温暖的世界
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
【数字IC手撕代码】Verilog固定优先级仲裁器|题目|原理|设计|仿真
How to save a section of pages in a PDF as a new PDF file
Arduino框架下 ESP32看门狗使用示例
多主复制下处理写冲突(4)-多主复制拓扑
数据可视化之百变柱状图
Line generation 005
最后写入胜利(丢弃并发写入)
C语言可以应用在哪些领域?
我们擅长的地方很多
数学建模学习(76):多目标线性规划模型(理想法、线性加权法、最大最小法),模型敏感性分析
ffmpeg基本命令
gergovia's deal tijie
不会多线程还想进 BAT?精选 19 道多线程面试题,有答案边看边学
多主复制的适用场景(1)-多IDC









