用c++找到4个向量的所有可能的组合

Finding all possible Combinations of 4 Vectors with C++

本文关键字:组合 有可能 向量 c++ 找到 4个      更新时间:2023-10-16

我想根据用户输入找到四个基向量(维数= 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的所有可能组合。