本文主要是介绍2017蓝桥杯官方模拟题 排列序数(康托展开),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
思路:
康托展开模板题
代码:
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define N 500000
#define inf 0x3f3f3f3f
#define M 1000000+2000
#define LL long long
using namespace std;
char s[15];
LL num[20];
LL jc(LL n)
{if(n==1)return 1;return n*jc(n-1);
}
void init()//打表阶乘
{for(LL i=1;i<=18;i++)num[i]=jc(i);
}
LL work(char s[],LL n)
{LL ans=0;for(LL i=0; i<n; i++){LL tmp=0;for(LL j=i+1; j<n; j++)if(s[j]<s[i])tmp++;//是第几大的元素ans+=tmp*num[n-i-1];}return ans;
}
int main()
{init();char s[20]="bckfqlajhemgiodnp";printf("%lld\n",work(s,17));return 0;
}
这篇关于2017蓝桥杯官方模拟题 排列序数(康托展开)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!