Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 矩阵计数实验室_C++_Recursion_Matrix_Permutation - Fatal编程技术网

C++ 矩阵计数实验室

C++ 矩阵计数实验室,c++,recursion,matrix,permutation,C++,Recursion,Matrix,Permutation,您的工作是编写一个名为matrix_enum的程序,它接受三个命令行参数:W、E和一个“x”或一个“h”。您的程序将按任意顺序枚举W和E的所有矩阵,并以以下两种格式之一打印出来: 如果最后一个参数是“x”,那么您将按以下格式打印矩阵。您应该将每个矩阵打印为W行W个字符,这些字符是“.”、“X”或“E”。在每个矩阵之后,打印一个空行 如果最后一个参数是“h”,则将每个矩阵的每一行转换为一个整数,并以十六进制打印该整数,不带前导0和前导“0x”。如果行中的元素i是“X”或“E”,则将数字的第i位设

您的工作是编写一个名为matrix_enum的程序,它接受三个命令行参数:W、E和一个“x”或一个“h”。您的程序将按任意顺序枚举W和E的所有矩阵,并以以下两种格式之一打印出来:

  • 如果最后一个参数是“x”,那么您将按以下格式打印矩阵。您应该将每个矩阵打印为W行W个字符,这些字符是“.”、“X”或“E”。在每个矩阵之后,打印一个空行
  • 如果最后一个参数是“h”,则将每个矩阵的每一行转换为一个整数,并以十六进制打印该整数,不带前导0和前导“0x”。如果行中的元素i是“X”或“E”,则将数字的第i位设置为1。否则,第i位为零。您将在自己的行上打印每个整数,并在每个矩阵的末尾打印一个空行

UNIX>矩阵_枚举2 0 x

十,

.X

.X

十,


UNIX>矩阵枚举2 0小时

一,

二,

二,

一,


这是我到目前为止的代码,但我不知道从这里去哪里

  5 class Matrix {
  6     public:
  7         int W;
  8         int E;
  9         int P;                      /* This is 'x' or 'h' */
 10         vector <int> Perm;          /* Permutation of 0 .. (W-1), for the 'X' elements. */
 11         vector <int> Non_X;         /* This is the row/col id of each of the non-X elements. */
 12         vector <int> E_ID;          /* This is the row/col id of the E elements */
 13         void Print();               /* Print the matrix defined by W, Perm and E_ID */
 14         void Permute(int index);    /* This is the recursive permuting method. */
 15         void Choose(int index);     /* This is the recursive n-choose-k method. */
 16 };
 17 
 18 void Matrix::Print() {
 19 
 20     for(int i = 0; i < Perm.size(); i++) {
 21 
 22 
 23     }
 24 }
 25
 26  void Matrix::Permute(int index) {
 27     //recursion to permute matrix
 28     string tmp;
 29 
 30     if(index == Perm.size()) {
 31         //Base case
 32         for (int i = 0; i < W; i++) {
 33             for(int j = 0; j < W; j++) {
 34                 //check if there is an 'x'
 35                 if(Perm.at(i) == j) {
 36                     continue;
 37                 }
 38 
 39                 Non_X.push_back(Perm.at(i));
 40                 Choose(0);
 41                 return;
 42             }
 43         }
 44     }
 45     //swap Perm[index] with Perm[i] and swap back
 46     for(int i = index; i < Perm.size(); i++) {
 47         //swap
 48         tmp = Perm[i];
 49         Perm[i] = Perm[index];
 50         Perm[index] = tmp;
 51 
 52         Permute(index+1);
 53 
 54         //backtrack
 55         tmp = Perm[i];
 56         Perm[i] = Perm[index];
 57         Perm[index] = tmp;
 58     }
 59 }
 60 
 61 void Matrix::Choose(int index) {
 62     //n choose k permutation
 63     //people = non x
 64     //eid = teams
 65     if (E - E_ID.size() == 0) {
 66         cout << E_ID[0];
 67         for (i = 1; i < E_ID.size(); i++) cout << " " << E_ID[i];
 68         cout << endl;
 69         return;
 70     }
 71 
 72     if ((E - E_ID.size()) > Non_X.size() - index) return;
 73 
 74     E_ID.push_back(Non_X[index]);
 75     Choose(index+1, (E - E_ID.size())-1);
 76     E_ID.pop_back();
 77 
 78     Choose(index+1, (E - E_ID.size()));
 79 
 80     // call Print();
 81 }
5类矩阵{
6公众:
7 int W;
8 int E;
9 int P;/*这是“x”或“h”*/
10矢量置换;/*0..(W-1)的置换,用于“X”元素*/
11 vector Non_X;/*这是每个非X元素的行/列id*/
12向量E_ID;/*这是E元素的行/列ID*/
13 void Print();/*打印由W、Perm和E_ID定义的矩阵*/
14 void置换(int-index);/*这是递归置换方法*/
15 void Choose(int index);/*这是递归的n-Choose-k方法*/
16 };
17
18无效矩阵::打印(){
19
20表示(int i=0;i66下一步你需要创建一个使用类矩阵的。确保包括a)测试输入和b)输入对应的输出,以及c)描述什么不起作用/而且行号在这个代码示例中比有用的更具破坏性。如果有人想要复制、编译和玩代码,那么必须删除行号是相当繁琐的首先是所有这些行号。事实上,行号对于注释中的各行来说非常漂亮。为了防止复制/粘贴代码后出现编译问题,可以将它们格式化为C风格的注释。(记事本++以及VS2013和更新版本提供了块编辑模式,这将使这项工作变得简单。其他编辑器/IDE可能提供类似的功能。或者只使用
sed
…;-)