C++之——实现排列组合

当涉足算法领域,排列组合应该是最基础的。最近遇到了,作个记录,小白看看,大神绕路

一、补充公式

排列的定义:从n个不同元素中,任取m(m≤n,m与n均为自然数,下同)个元素按照一定的顺序排成一列,叫做从n个不同元素中取出m个元素的一个排列;从n个不同元素中取出m(m≤n)个元素的所有排列的个数,叫做从n个不同元素中取出m个元素的排列数,用符号 A(n,m)表示,公式如下:

    A(n,m) = n!/(n-m)!

组合的定义:从n个不同元素中,任取m(m≤n)个元素并成一组,叫做从n个不同元素中取出m个元素的一个组合;从n个不同元素中取出m(m≤n)个元素的所有组合的个数,叫做从n个不同元素中取出m个元素的组合数。用符号 C(n,m) 表示:

  C(n,m) = n!/(m!*(n-m)!)   (n>=m)

二、代码实现

2.1 c++代码实现排列

话不多说,直接上代码:


#include <iostream>
using namespace std;

//交换函数
void swap(int& a, int& b )
{
	int temp = a;
	a = b;
	b = temp;
}
/*入参:
	begin 要排列的起始位置
	end   要排列的结束位置
	a[]	 要排列的数组
*/
void permutate(int begin, int end, int a[])
{
	if(begin == end)//已经到了最后一个位置,进行输出
	{
		for(int i=0; i < end;i++)
		{
			cout<<a[i];
		}
		cout<<endl;
	}
	for(int i= begin; i < end; i++)
	{
		swap(a[begin],a[i]);
		permutate(begin + 1,end,a);//递归下一个位置
		swap(a[i],a[begin]);//回溯
	}
}

int main()
 {
	int a[5] = {1,2,3,4,5};
	permutate(0,3,a);//对数组中前三个数进行排列
	return 0;
}

输出为:

123
132
213
231
321
312

当然也可进行简单的优化:

template<class T,int N>
void permutate(int begin, int end, T (&a)[N])
{
	if(0 > begin || begin > end || end > N)
	{
		cout<<"输入参数错误"<<endl;
		return;
	}
	//输出情况
	if(begin == end)
	{
		for(int i=0; i < end;i++)
		{
			cout<<a[i];
		}
		cout<<endl;
	}
	for(int i= begin; i < end; i++)
	{
		swap(a[begin],a[i]);
		permutate(begin + 1,end,a);
		swap(a[i],a[begin]);
	}
}
2.2 c++代码实现组合

代码:

#include <iostream>
using namespace std;
void combinate(int iPos, int iProc, int iTol, int iKey, int data[],int des[])
{
	if(iProc > iTol)
	{
		return;
	}
	if(iPos == iKey)
	{
		for(int i = 0;i < iKey; i++)
		{
			cout<<des[i]<<" ";
		}
		cout<<endl;
		return;
	}
	else
	{
		combinate(iPos,iProc+1,iTol,iKey,data,des);
		des[iPos] = data[iProc];
		combinate(iPos+1,iProc+1,iTol,iKey,data,des);
	}
}
int main()
 {
	int a[6] = {1,2,3,4,5,6}, b[5];
	combinate(0, 0, 5, 3 , a,b);
	//如果需要终端输入
	/*
	cin>>n>>k;
	if(n > k > 0)
	{
		int data[n],temp[n];
		for(i = 0; i < n; i++ )
		{
			data[i] = i+1;
		}
		combinate(0, 0, n, k, data, temp);
	}
	*/
	return 0;
}

输出为:

3 4 5 
2 4 5 
2 3 5 
2 3 4 
1 4 5 
1 3 5 
1 3 4 
1 2 5 
1 2 4 
1 2 3 

平时工作一般用不到这些,最近自己琢磨发现虽然逻辑简单,但是写起来还确实不知道怎么下手。。。
所以不能羡慕算法大神年薪几十万上百万,毕竟得脑子好用不是?

===============
想分一杯羹,还是尽早学习,共勉!

以下是一个简单的GAT(Graph Attention Network)图神经网络的代码示例: ```python import torch import torch.nn as nn import torch.nn.functional as F class GATLayer(nn.Module): def __init__(self, in_features, out_features, dropout=0.6, alpha=0.2): super(GATLayer, self).__init__() self.dropout = dropout self.alpha = alpha self.W = nn.Linear(in_features, out_features, bias=False) self.a = nn.Linear(2*out_features, 1, bias=False) def forward(self, X, adj_matrix): h = self.W(X) N = h.size(0) a_input = torch.cat([h.repeat(1, N).view(N*N, -1), h.repeat(N, 1)], dim=1).view(N, -1, 2*h.size(1)) e = F.leaky_relu(self.a(a_input).squeeze(2), negative_slope=self.alpha) zero_vec = -9e15*torch.ones_like(e) attention = torch.where(adj_matrix > 0, e, zero_vec) attention = F.softmax(attention, dim=1) attention = F.dropout(attention, p=self.dropout, training=self.training) h_prime = torch.matmul(attention, h) return F.elu(h_prime) class GAT(nn.Module): def __init__(self, in_features, hidden_features, out_features, num_layers, dropout=0.6, alpha=0.2): super(GAT, self).__init__() self.hidden_features = hidden_features self.num_layers = num_layers self.layers = nn.ModuleList([GATLayer(in_features, hidden_features, dropout=dropout, alpha=alpha)]) self.layers.extend([GATLayer(hidden_features, hidden_features, dropout=dropout, alpha=alpha) for _ in range(num_layers-2)]) self.layers.append(GATLayer(hidden_features, out_features, dropout=dropout, alpha=alpha)) def forward(self, X, adj_matrix): h = X for layer in self.layers: h = layer(h, adj_matrix) return h ``` 这是一个简单的GAT图神经网络的实现,包括了GATLayer和GAT两个类。GATLayer定义了一个GAT层的操作,GAT则将多个GAT层串联起来构成整个图神经网络。其中,in_features表示输入特征的维度,hidden_features表示隐层特征的维度,out_features表示输出特征的维度,num_layers表示GAT层数,dropout表示dropout率,alpha表示LeakyReLU的斜率。 希望这个代码示例对你有帮助!如有任何问题,请随时提问。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值