当前位置:网站首页>7.18 number of square arrays
7.18 number of square arrays
2022-07-22 19:36:00 【Zhou Xuanhong】
Topic link
Question making
30min too 3/10
The reason for the error is that repeated permutations are not considered , This is also the focus of this topic
Code and solution
This problem is actually pruning a full arrangement
In order to satisfy the question : The first condition of pruning is
- Need to satisfy the complete square
- Non repeating numbers are required
The first one is easy to satisfy , The second point is to consider, such as
1 1 2 (1 2 3) Serial number in brackets
The arrangement of the numbers above is :
1 1 2 (1 2 3)
1 1 2 (2 1 3)
Such repetition can be solved in this way :
Default for the same , Be sure to put the small serial number in front , If you traverse to a certain number , But it is the same as its value, but the number in front of it has not been used , At this time, this number cannot be used .
Then sort by logarithm first , In this way, the same numbers will be put togetherif((a[i]==a[i-1]&&e[i-1]==1))
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
/* First row full arrangement , Prune in the process of full arrangement */
const int N=15;
int n;
int a[N];
int e[N];
int q[N];
long long ans=0;
int judge(int x){
// Used to judge x Is it a complete square
if(sqrt(x)==(int)sqrt(x))
return 1;
return 0;
}
void dfs(int q[], int layer){
if(layer==n){
ans++;
return ;
}
for(int i=1;i<=n;i++){
if(e[i]==0&&judge(q[layer]+a[i])){
if((a[i]==a[i-1]&&e[i-1]==1))
continue;
e[i]=1;
q[layer+1]=a[i];
dfs(q,layer+1);
e[i]=0;
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
q[1]=a[i];
e[i]=1;
dfs(q,1);
e[i]=0;
}
cout<<ans<<endl;
return 0;
}
Postscript
Playing football
边栏推荐
- Force deduction solution summary 731- my schedule II
- Call() and apply()
- LeetCode 每日一题 2021/12/20-2021/12/26
- 最强屏幕工具ShareX v14.0.1
- Force deduction solution summary 498 diagonal traversal
- pytest接口自动化测试框架 | 接口自动化至yaml数据驱动
- Force deduction solution summary 735 planetary collision
- Flutter premier programme Hello world!
- Force deduction solution summary 241- design priority for operation expression
- Has the climate changed in Hebei and Zhejiang?
猜你喜欢
JS advanced - understanding of functions
Industrial router oilfield wireless monitoring
Grafana panel - override field values
Jackson parsing JSON detailed tutorial
“35岁,我退休了”:关于中年危机,这是最靠谱的回答
Flutter premier programme Hello world!
Mail Informer
SQL Design Teaching Management Library
由浅入深详解NLP中的Adapter技术
QT中多线程的使用
随机推荐
Introduction to arrays
The force deduction method summarizes the number of 1252 odd value cells
4G工业路由器大气环境监测方案
Help brand insight -- Analysis of consumers' emotional behavior
7.21 排列与二进制
LeetCode 每日一题 2022/2/7-2022/2/13
Ten year structure five year Life-05 first business trip
工业路由器油田无线监控
Solution to unsuccessful (invalid) password modification of MySQL
Don't let fear of marriage kill your happiness!
pytest接口自动化测试框架 | 为什么要做pytest插件的二次开发
十年架构五年生活-05第一次出差
用指针遍历数组
JS advanced - basic data type and reference data type
由浅入深详解NLP中的Adapter技术
30出头成为复旦博导,陈思明:敲代码和写诗,我两样都要
不再执着于用产品说话,苹果库克加大美国反垄断游说投入:今年上半年支出 460 万美元
Force deduction solution summary 1175 prime number arrangement
Has the climate changed in Hebei and Zhejiang?
STM32中什么是预分频