#include <iostream> #include<stdio.h> #include<math.h> using namespace std; int n,a[200],plan; void dfs(int x){ if(x>n){ plan++; return; } for(int i=1;i<=n;i++){ a[x]=i; int flag=0; for(int j=1;j<=x-1;j++){ if(a[x] == a[j] || fabs(x-j)==fabs(a[x]-a[j])){ flag=1; break; } } if(flag==0) dfs(x+1); } } int main() { while(scanf("%d",&n)!=EOF){ plan=0; for(int i=1;i<20;i++) a[i]=0; dfs(1); cout<<plan<<endl; } return 0; }
时间: 2023-12-29 16:06:34 浏览: 26
这段代码是经典的 N 皇后问题的解法,通过深度优先搜索来枚举每一行皇后所在的列数,同时判断该列是否合法(即是否已经有皇后在该列或者对角线上)。
其中,变量 n 表示皇后的数量,a 数组用来存储每一行皇后所在的列数,plan 表示方案数。
需要注意的是,每次递归前需要将 a 数组清零,否则可能会影响结果。
相关问题
#include<iostream> #include<cmath> #include<stdio.h> #include<iomanip> #include<math.h> using namespace std; int main() { char ch = 'y'; do { cout << "Enter month in 2021 to display calendar(1-12):"; int m = 0; cin >> m; if (m < 13) { for (i
这段代码实现了一个生成2021年月历的程序。具体来说,它首先让用户输入月份m的值,然后使用for循环来生成该月份的月历。在循环中,使用了一个变量d来表示当前日期,使用了一个变量w来表示当前日期是星期几,根据w的值来控制输出格式。在输出日期时,使用了setw函数来控制输出宽度,使得输出的日期能够对齐。
需要注意的是,这段代码中存在一些问题:
1. 输出格式不规范:代码中输出的月历格式不够美观,建议使用更加合理的输出方式,使得月历更加清晰易读。
2. 变量命名不规范:变量名m、d、w等不够具有描述性,建议使用更明确的名称。
3. 代码风格不规范:代码中缺少缩进、换行等格式控制,不易于阅读。
因此,建议在使用这段代码之前进行修正和测试。修正后的代码如下所示:
#include <iostream> #include <stack> #include <algorithm> using namespace std; int main() { char num[40005]; int n; while(cin>>n) { stack <int> s; int a; if(n == 0) cout<<0<<endl; else { while(n!=0) { a = abs(n%(-2)); s.push(a); n = (n-a) / (-2); } } while(!s.empty()) { cout<<s.top(); s.pop(); } cout<<"\n"; } return 0; }将这个c++的代码改为c语言的代码
很好,我已经接收到你的问题。以下是将该C++代码转换为C语言代码的过程。
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
char num[40005];
int n;
while(scanf("%d", &n) != EOF) {
int s[40005], top = -1, a;
if(n == 0)
printf("0\n");
else {
while(n != 0) {
a = abs(n % (-2));
s[++top] = a;
n = (n - a) / (-2);
}
}
while(top >= 0)
printf("%d", s[top--]);
printf("\n");
}
return 0;
}
```
注意,由于C语言中没有stack容器,因此我们使用数组来模拟栈的操作。另外,C语言中没有cin和cout,我们需要使用scanf和printf函数来进行输入输出。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)