1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1022
2、题目大意:
给定两个数k n
用1-k的数组成一个n个数的序列,如果这个序列每两个相邻的数相差<=1,就记为是tight,求这种序列占总序列的比率
3、题目:
Input is a sequence of lines, each line contains two integer numbers k and n, 1 <= n <= 100. For each line of input, output the percentage of tight words of length n over the alphabet {0, 1, ... , k} with 5 fractional digits.
4 1 2 5 3 5 8 7
100.00000 40.74074 17.38281 0.101304、代码:
#include
double dp[110][12];//第i个位置填充j的概率
int main()
{
int k,n;
while(scanf("%d%d",&k,&n)!=EOF)
{
if(k<=1)
printf("100.00000\n");
else
{
for(int i=0;i<=k;i++)
dp[1][i]=100.0/(k+1);
for(int i=2;i<=n;i++)
{
for(int j=0;j<=k;j++)
{
if(j==0)
{
dp[i][j]=1.0/(k+1)*(dp[i-1][j]+dp[i-1][j+1]);
}
else if(j==k)
{
dp[i][j]=1.0/(k+1)*(dp[i-1][j-1]+dp[i-1][j]);
}
else
{
dp[i][j]=1.0/(k+1)*(dp[i-1][j-1]+dp[i-1][j]+dp[i-1][j+1]);
}
}
}
double ans=0;
for(int i=0;i<=k;i++)
{
ans+=dp[n][i];
}
printf("%.5lf\n",ans);
}
}
return 0;
}