一、热身 [Cloned] K - next_permutation

原题:

Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess." 

"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......" 
Can you help Ignatius to solve this problem? 

题意:

输入N和K,要找出1,2,3,....,N组成的第K字典序小的序列

题解:

这个题想了一些时间,本来以为是数学问题找排序。结果发现头文件里有next_permutation函数,可以直接找出全排列,强大强大。找第k个直接排序k次,然后输出

代码:AC

#include<iostream>
#include<algorithm>
using namespace std;
int arr[1111];
int main()
{
	int N,K;
	while(cin>>N>>K)
	{
		int i;
		for(i=0;i<N;i++)
		{
			arr[i]=i+1;
		}
		K--;
		while(K--)
		{
			next_permutation(arr,arr+N);
		}
		for(i=0;i<N-1;i++)
		{
			cout<<arr[i]<<" ";
		}
		cout<<arr[i]<<endl;
	}
	return 0;
}

 

static int repeater_l2_proc(struct repeater_info *rp_info, struct sk_buff **skb, int send) { struct ethhdr *eh = NULL; u8 *l3_data = NULL; int type = 0; int i = 0; int ret = -1; u8 *l2_data = NULL; u8 *ptype = NULL; int data_len = 0; struct sk_buff *new_skb = NULL; if (debug_open) { return 0; } if ((*skb)->len < sizeof(struct ethhdr)) { return -1; } eh = (struct ethhdr *)((*skb)->data); type = ntohs(eh->h_proto); spin_lock_bh(&g_repeater_lock); /* Only handle bridge forward */ if (!(rp_info->vif_dev->priv_flags & IFF_BRIDGE_PORT)) { // Warnning ip error spin_unlock_bh(&g_repeater_lock); return 0; } if (ether_addr_equal(eh->h_source, rp_info->if_mac)) { repeater_dbg("%s smac %pM -> dmac %pM proto 0x%X\n", send ? "send" : "recv", &eh->h_source, &eh->h_dest, type); spin_unlock_bh(&g_repeater_lock); return send ? 0 : -2; } spin_unlock_bh(&g_repeater_lock); if (send && ((skb_shared(*skb) || (skb_cloned(*skb))) && is_multicast_ether_addr(eh->h_dest))) { new_skb = skb_copy(*skb, GFP_ATOMIC); if (unlikely(!new_skb)) { repeater_err("mem is not enough.\n"); return -1; } kfree_skb(*skb); *skb = new_skb; } l2_data = (*skb)->data; ptype = (*skb)->data + offsetof(struct ethhdr, h_proto); /* 802.2 Handle */ if (type <= ETH_DATA_LEN && data_len >= ETH_HLEN + SNAP_HDR_LEN) { u8 *llc = ptype; repeater_dbg("type 0x%X\n", type); if (memcmp(g_llc_snap_hdr, llc, SNAP_HDR_LEN)) { repeater_dbg("llc packet\n"); if (send && !memcmp(g_llc_hdr_stp, llc, SNAP_HDR_LEN)) { repeater_dbg("stp packet\n"); ether_addr_copy(l2_data + ETH_ALEN, rp_info->if_mac); } return 0; } ptype = llc + SNAP_HDR_LEN; } if (htons(ETH_P_8021Q) == (*(u16 *)ptype)) { ptype += sizeof(struct vlan_hdr); } l3_data = ptype + ETHER_PROTO_LEN; data_len = (*skb)->len - (l3_data - l2_data); type = ntohs(*(u16 *)ptype); for (i = 0; i < ARRAY_SIZE(repeater_prot_handle); i++) { if (repeater_prot_handle[i].type != type) { continue; } spin_lock_bh(&g_repeater_lock); ret = (repeater_prot_handle[i].prot_proc)(rp_info, l2_data, l3_data, data_len, send); spin_unlock_bh(&g_repeater_lock); } if (ret < 0) { repeater_dbg("%s proto 0x%X can not proccess.\n", send ? "send" : "recv", type); } return ret; }这个函数在做什么
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值