用c++找到4个向量的所有可能的组合
Finding all possible Combinations of 4 Vectors with C++
我想根据用户输入找到四个基向量(维数= 4)的所有可能组合。应该允许重复。
。我们称这些向量为a, b, c和d。
如果用户输入N=3,则组合为:
aaa艺术展aac格式...ddd
我已经尽力了,但是我对c++还不是很熟悉。
应用程序还应计算基向量的乘法(即a*a*a)并存储结果。
我肯定在论坛之前看了一下,但发现文章关于组合整数或向量的元素。
谢谢你的帮助。
如果您的程序是基于linux的(例如,您可以访问bash类型的shell),那么您可以通过输入命令:"echo {a,b,c,d}{a,b,c,d}"来获得N=3的所有组合,例如a,b,c,d} -它将打印出上述所有组合:aaa, aab…等。
然后您可以捕获该输出并逐字母解析它,并将基向量(例如dim = 4时的(1,1,1,1))乘以与该字母对应的每个向量。但话说回来,向量相乘是什么意思?你是说求a*a*a的大小?因为你不能把(1x4)向量相乘你只能取它们的点积。(所以a*a*a的唯一可能的解释似乎是|a|^3)
调用echo命令示例:
#include <stdio.h>
#include <string>
#include <vector>
int n; //user given - n>1
Vector4D* vectors[n]; // Or some other name for the class
// Have the user input the vectors
std::string base = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
std::string list = "{"+base.substr(0,2*n+1)+"}";
std::string query = "echo ";
for(int i = 0; i<4; i++){
query += list;
}
File *file;
file=popen(query.c_string(), "r");
char combo[4];
std::vector<Vector4D> results;
while(fscanf(file,"%s", combo)){
Vector4D result(1,1,1,1); //our default (identity) vector
for(int i = 0; i<4; i++){
result = Vector4D.multiply(result, Vectors[combo[i]-'a']);
}
results.push_back(result);
}
fclose(file);
像这样的东西,虽然a)未经测试b)不完整c)只适用于多达26个向量的样本大小(如果包括大写字母,则为52)
我会考虑使用多维数组(在本例中是二维数组)。
现在,我的编程水平也不是很好,所以我的程序往往有点笨拙,但希望你能得到一个想法,我在下面的伪代码示例中所说的!
以用户输入的数字为基础计算巢的计数:
user enters 4
array{ar1{a,b,c,d},ar2{a,b,c,d},ar3{a,b,c,d}ar4{a,b,c,d}}
create counter var for each array (c1, c2, c3, c4)
loop increment counters c1-4 for each letter (a-b)
print value from array table.
我知道这很潦草,但这是我能想到的最好的东西(我已经快3年没有编程了!)哈哈),但一定要看看多维数组:
http://www.tenouk.com/clabworksheet/labworksheet10.html祝你好运!
由于这是作业,我就不给你们写完整的解决方案了。检查伪代码:
all(&srcvectors, depth, temp, &resultvectors)
if (depth == 0)
resultvectors.push(temp);
else
for (int i=0; i < 4; i++)
all(srcvectors, depth-1, crossproduct(temp, srcvectors[i]), resultvectors);
你必须从外部for循环调用它,传递每个srcvector作为temp.你还必须弄清楚你必须传递的初始深度是多少(试着推断它,不要使用试错法)。
设置两个4 * 4的for循环。这将运行在每一个可能的组合。
vector<char> temp;
temp.push_back('a');
temp.push_back('b');
temp.push_back('c');
temp.push_back('d');
for(int i = 0; i < 4; i++)
{
for(int k = 0; k < 4; k++)
cout << temp[i] << temp[k] << endl;
}
添加更多for循环以增加长度。现在它一次只能输出2个组合。
例如:vector<char> temp;
temp.push_back('a');
temp.push_back('b');
temp.push_back('c');
temp.push_back('d');
for(int j = 0; j < 4; j++)
{
for(int i = 0; i < 4; i++)
{
for(int k = 0; k < 4; k++)
{
cout << temp[j] << temp[i] << temp[k] << endl;
Sleep(25);
}
}
}
system("PAUSE");
将输出长度为3的所有可能组合。
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 有可能使shared_ptr协变吗
- 有可能在信号处理程序中设置promise吗
- 是否有可能实现O(N)时间和O(1)空间解决方案,以实现C++中的字符串循环移位
- 是否有可能构建面向Linux和Windows的.Net Core C++ / CLI应用程序?
- 是否有可能使用debug_info获取ELF文件的源代码?
- C++,是否有可能/如何定义在.h和.cpp源文件中调用函数的类构造函数
- 有可能在C++中有类的查找表吗
- 是否有可能让 c++ dll 在后台运行 python 程序并让它填充向量图?如果是这样,如何?
- 向量的大小是否有可能为 1 但其中的元素数量为零?
- 是否有可能编写新的叮当声现代化规则?
- 是否有可能通过指向另一个未关联的子对象的指针来获取指向一个子对象的指针?
- 是否有可能通过演绎指南实现整个 std::make_tuple 功能?
- 是否有可能在没有复制的情况下传递 std::vector<int> 作为参数来获得 std::vector<std::array<int, 3>>?
- 如果我们从每个 Y 向量中选择一个值,则 X 数字的每个组合都可能
- 是否有可能具有放入容器的移动操作的类型?
- 是否有可能通过溢出 C 中的第一个元素来写入数组第二个元素
- 是否有可能组合对称的代码片段