根据上一次有序漏斗算法的基础上增加了20个路径来进行动态的模拟,并且可以自行选择或者设定路径,每个路径最多8个节点。此次的算法将每个行动设定为单个的字母,实时的输出完成有效的路径的次数。
具体代码如下:
//多模式有序漏斗算法
#include <iostream>
#include <string>
#include<vector>
using namespace std;
//定义好model,其中每个model最多存在8个节点,此处存在20个model,可以无限添加。
const char model[20][8] =
{ 'A','B','C','D','E','F','G','H',
'A','C','B','D','E','F','G','H',
'A','B','D','C','E','F','G','H',
'A','B','C','E','D','F','G','H',
'A','B','C','D','F','E','G','H',
'A','B','C','D','E','G','F','H',
'A','B','C','D','E','F','H','G',
'A','D','C','B','E','F','G','H',
'A','E','C','D','B','F','G','H',
'A','F','C','D','E','B','G','H',
'A','G','C','D','E','F','B','H',
'A','H','C','D','E','F','G','B',
'A','B','E','D','C','F','G','H',
'A','B','F','D','E','C','G','H',
'A','B','G','D','E','F','C','H',
'A','0','0','0','0','0','0','0',
'A','B','0','0','0','0','0','0',
'A','B','C','0','0','0','0','0',
'A','B','C','D','0','0','0','0',
'A','B','C','D','E','0','0','0',
};
//初始定义好节点标志位i0-i7
int i[8] = {0,0,0,
0,0,0,
0,0};
//有序漏斗算法,但是因为case后面只能添加的是常量,所以选择if elseif 。但是实际上其实可以先用变量转化以后在用case
int funnel(char str[1],int n,int modelNum)
{
if (str[0] == model[modelNum - 1][0])
i[0]++;
else if (str[0] == model[modelNum - 1][1] && i[0] > i[1])
i[1]++;
else if (str[0] == model[modelNum - 1][2] && i[1] > i[2])
i[2]++;
else if (str[0] == model[modelNum - 1][3] && i[2] > i[3])
i[3]++;
else if (str[0] == model[modelNum - 1][4] && i[3] > i[4])
i[4]++;
else if (str[0] == model[modelNum - 1][5] && i[4] > i[5])
i[5]++;
else if (str[0] == model[modelNum - 1][6] && i[5] > i[6])
i[6]++;
else if (str[0] == model[modelNum - 1][7] && i[6] > i[7])
i[7]++;
return 0;
}
//输入model数,判断是否合适,
int main()
{
int modelNum;
cout << "请输入模型数" << endl;
cin >> modelNum;
if (modelNum < 0 || modelNum>20)
{
cout << "请输入0-20的数字" << endl;
cout << "请输入模型数" << endl;
cin >> modelNum;
if (modelNum < 0 && modelNum>20)
return 0;
}
//判断节点数
int n = 0; int inputStr = 0;
for (int i = 0; i < 8; i++)
{
if (model[modelNum-1][i] != '0')
n++;
}
cout << endl << "输入的节点数:" << n << endl;
//取一个字符,进入while循环,最多连着接受10000个字符
char str[1];//字符数组,用于存放字符串的每一个字符
while (10000>=inputStr)
{
cout << "Please input a string" << endl;
cin >> str; //输入字符
cout << "The string you input is:" << str << endl;
funnel(str, n, modelNum);
//此时只输出完成model中过程的次数
cout << endl << "完成的次数:" << i[n-1] << endl;
inputStr++;
}
cout << endl << "字符太长了" << endl;
}
在model字符组中设置路径模型
首先进行模式的设置路径
然后单次输入字符,实时输出完成次数。
网友评论