我正在尝试计算递归排列函数中的调用次数.
我编写了一个用所有排列填充队列的函数,但我似乎无法弄清楚如何保持准确的计数.
最终我希望函数返回lbound和ubound参数指定的permuatations的子集,并且这样做我认为我需要保留一个内部计数.
使用返回队列的大小将不起作用,因为我希望该函数能够处理太大而无法保存在内存中的排列.
对于此代码,我希望将计数返回为100.
#include <vector>
#include <iostream>;
using namespace std;
int& Permutations(vector<vector<int>> param, vector<vector<int>> &perm, int index=0)
{
static vector<int> iter;
static int count = 0;
if (index == param.size())
{
perm.push_back(iter); // add permutation to queue
count++;
return count;
}
for (int i=param[index][0]; i<=param[index][1]; i+=param[index][2])
{
if (iter.size() > index) iter[index] = i;
else iter.push_back(i);
Permutations(param, perm, index+1); // recursive function
}
}
void main()
{
vector<vector<int>> params; // vector of parameter vectors
vector<int> param1, param2;
int …Run Code Online (Sandbox Code Playgroud) 我有一张桌子,只有一列由5种颜色组成 -
colour
-------
red
black
white
green
orange
Run Code Online (Sandbox Code Playgroud)
我希望得到所有的组合
(红色,橙色)(黑色,白色)...等等除了相同的那些.我试图与自己交叉加入表.
select *
from table1 cross join table1
Run Code Online (Sandbox Code Playgroud)
但我没有得到所需的答案.它返回了所有的组合.也是相同的那些.我能得到它吗?有没有其他方法可以做到这一点而不创建另一个表???
我有很多名字.我想将函数应用于名称的所有排列,以找出所有可能对的所有可能边的总和:
shapes<- c("Square", "Triangle","Octagon","Hexagon")
sides<-c(4,3,8,6)
shapescount<-combn(shapes, 2)
shapescount
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "Square" "Square" "Square" "Triangle" "Triangle" "Octagon"
[2,] "Triangle" "Octagon" "Hexagon" "Octagon" "Hexagon" "Hexagon"
Run Code Online (Sandbox Code Playgroud)
如何为所有排列添加所有边?
我有一个二进制数,例如1010,我想知道该二进制数的唯一排列数.
例如,1010有6个独特的排列:
而1000有4个独特的排列:
那么,给定一个长度X为2 N和X-N0 的二进制字符串,有多少个唯一的排列?
我一直在努力实现堆算法的递归版本.以下是伪代码的链接:http://en.wikipedia.org/wiki/Heap%27s_algorithm
在我到达递归部分之前,一切都很顺利.我知道我还没有交换元素,但我没有那么远.在我使用gcc调试器告知我存在分段错误之前,运行失败而没有显示错误.这是我的代码:
#include <string>
#include <iostream>
using namespace std;
string* permute(int n, string array[2]){
if (n==1){
return array;
}
else{
for(int c=1; c<=n;c++){
permute(n--,array);
}
}
}
int main() {
string array[2]={"a","b"};
permute(2,array);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的data.frame:
DqStr <- "Group q Dq SD.Dq
1 -3.0 0.7351 0.0067
1 -2.5 0.6995 0.0078
1 -2.0 0.6538 0.0093
2 -3.0 0.7203 0.0081
2 -2.5 0.6829 0.0094
2 -2.0 0.6350 0.0112"
Dq1 <- read.table(textConnection(DqStr), header=TRUE)
Run Code Online (Sandbox Code Playgroud)
我想随机化组成员身份,但仅限于具有相同Dq1 $ q值的行
g <-unique(Dq1$q)
Dq2<- data.frame()
for(n in g)
{
Dqq <- Dq1[Dq1$q==n,]
Dqq$Group <-sample(Dqq$Group)
Dq2 <- rbind(Dq2,Dqq)
}
Run Code Online (Sandbox Code Playgroud)
这也可以通过plyr来完成
library(plyr)
ddply(Dq1,.(q), function(x) { x$Group <- sample(x$Group)
data.frame(x)})
Run Code Online (Sandbox Code Playgroud)
因为我必须重复这几千次,我想知道是否有更好(更快)的方法来做到这一点.
我想根据它们的索引模3来置换列表的元素,例如列表:
[0,1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)
应重新订购:
[0,3,6,1,4,7,2,5,8]
Run Code Online (Sandbox Code Playgroud)
一般来说:
[A0, A1, A2, A3, A4, A5, A6, A7, A8]
Run Code Online (Sandbox Code Playgroud)
应成为:
[A0, A3, A6, A1, A4, A7, A2, A5, A8]
Run Code Online (Sandbox Code Playgroud)
我试过使用以下代码:
def arr_sort(arr, algo):
arrtmp = arr
arrlen = len(arr)
if algo == 1:
return arr
if algo == 2:
count = 0
while count < (arrlen - 1):
for index, val in enumerate(arr):
if index % 3 == 0:
arrtmp[count] = val
count += 1
for index, val in enumerate(arr):
if index % 3 == …Run Code Online (Sandbox Code Playgroud) 问候Delphian堆垛机.
我搜索了网站,所有"排列等级和排名"相关的讨论,找不到符合我需求的那个.
在德尔福:
有一个数组:
Members: array [0..3] of Byte = (0,1,2,3);
Run Code Online (Sandbox Code Playgroud)
如果想要迭代由3个元素组成的所有不同排列,可以估计结果列表将由24行组成,按字典顺序排列为:
0 012
1 013
2 021
3 023
4 031
5 032
6 102
7 103
8 120
9 123
10 130
11 132
12 201
13 203
14 210
15 213
16 230
17 231
18 301
19 302
20 310
21 312
22 320
23 321
Run Code Online (Sandbox Code Playgroud)
可以使用"n选择k"公式计算列表的大小,其中"n"表示成员数,"k"表示选择数:
p(n,k) = n! / (n-k)!
p(4,3) = 4! / (4-3)! = (4 x 3 x 2 x 1) / …Run Code Online (Sandbox Code Playgroud) 给定一个numpy数组,如何在其中找到索引序列,以便对结果进行排序?
例如,给定x=[4,2,6],结果将是[1,0,2],因为[x[1],x[0],x[2]]是排序的.
我知道有很多可用的Python函数argsort()可以完成这项工作,但我需要自己实现这个排序功能.有什么建议?