我已经用C++代码编写了一个函数来解决八个皇后问题.该程序应打印出所有92种可能的解决方案.我只能跑到40岁.不知道问题出在哪里.尝试调试,但我仍然卡住了.
#include "stdafx.h"
#include <cmath>
#include <iostream>
using namespace std;
bool ok(int board[8][8]){
for(int c = 7; c > 0; c--){
int r = 0;
while(board[r][c] != 1 ){
r++;
} // while loop
for(int i = 1; i <= c; i++){
if(board[r][c-i] == 1)
return false;
else if (board[r-i][c-i] == 1)
return false;
else if (board[r+i][c-i] == 1)
return false;
} // for loop
} // for loop
return true;
} // ok
void print(int board[8][8], int …Run Code Online (Sandbox Code Playgroud) 我不得不使用goto语句编写类似的问题.现在我们被要求在不使用goto语句的情况下重写代码.我不知道如何开始这个程序.我使用goto粘贴以前的程序代码.
// Eight Queens problem using one dimesional array and goto statement
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int q[8];
q[0] = 0;
int c = 0;
int count = 0;
NC: //cout << "Next column\n" << "Column = " << c << endl;
c++;
if (c == 8) goto print;
q[c] = -1;
NR: //cout << "Next row\n" << "Row = " << q[c] << "\nColumn = " << c << endl;
q[c]++;
if (q[c] …Run Code Online (Sandbox Code Playgroud) 想对问题1的结果作出解释.
***1.以下方法的输出是什么?
public static void main(String[] args) {
Integer i1=new Integer(1);
Integer i2=new Integer(1);
String s1=new String("Today");
String s2=new String("Today");
System.out.println(i1==i2);
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1!=s2);
System.out.println( (s1!=s2) || s1.equals(s2));
System.out.println( (s1==s2) && s1.equals(s2));
System.out.println( ! (s1.equals(s2)));
}
Run Code Online (Sandbox Code Playgroud)
回答:
false
false
true
true
true
false
false
Run Code Online (Sandbox Code Playgroud)