我刚试过第一次编程访谈,其中一个问题是编写了一个给出7位数电话号码的程序,可以打印每个号码可能代表的所有可能的字母组合.
问题的第二部分就是如果这是一个12位数的国际号码呢?这会对你的设计产生什么影响.
我没有在采访中写的代码,但我得到的印象是他对此不满意.
做这个的最好方式是什么?
如何在C++中返回二维数组?
例如,我在java中有以下方法:
public static int[][] getFreeCellList(int[][] grid) {
// Determine the number of free cells
int numberOfFreeCells = 0;
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
if (grid[i][j] == 0)
numberOfFreeCells++;
// Store free cell positions into freeCellList
int[][] freeCellList = new int[numberOfFreeCells][2];
int count = 0;
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
if (grid[i][j] == 0) {
freeCellList[count][0] = i;
freeCellList[count++][1] = j;
}
return freeCellList;
}
Run Code Online (Sandbox Code Playgroud)
我试图用C++复制它.通常,我会传入我想要返回的2d数组作为C++中方法的参考参数.
但是,正如您在上面的方法中看到的那样,直到运行时才知道返回的数组的大小.
所以,在这种情况下,我猜我需要实际返回一个二维数组,对吧?
如果我有以下Java代码:
int[][] readAPuzzle()
{
Scanner input = new Scanner(System.in);
int[][] grid = new int[9][9];
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
grid[i][j] = input.nextInt();
return grid;
}
public static void main(String[] args) {
// Read a Sudoku puzzle
int[][] grid = readAPuzzle();
}
Run Code Online (Sandbox Code Playgroud)
如何将其转换为C++?我挂断了传递数组.这是我的尝试:
#include <iostream>
using namespace std;
const int puzzle_width = 9;
const int puzzle_height = 9;
void readAPuzzle(int (&grid)[puzzle_height][puzzle_width])
{
for(int i = 0; i < 9; i++)
for(int j = 0; j < …
Run Code Online (Sandbox Code Playgroud)