康拖展開的函數:
int Contor(char s[],int n)
{
int i, j,fac[]={1,1,2,6,24,120,720,5040,40320,362880};//階乘;
int ans=0;
for(i=0;i<n;i++)
{
int smaller=0;
for(j= i+1 ;j<n;j++)
{
if(s[i] > s[j])smaller++;
}
ans += smaller*fac[n-i-1];
}
return ans+1;
}
AC程式碼:
#include<stdio.h>
const int fac[]={1,1,2,6,24,120,720,5040,40320,362880};//階乘
char s[108];
int n;
int Contor(char s[],int n)
{
int i, j;
int ans=0;
for(i=0;i<n;i++)
{
//std::cout<<ans<<std::endl;
int smaller=0;
for(j= i+1 ;j<n;j++)
{
if(s[i] > s[j])smaller++;
}
ans += smaller*fac[n-i-1];
}
return ans+1;
}
int main()
{
scanf("%d", &n);
scanf("%s", s);
printf("%d", Contor(s, n));
}