Tight words (dp)

http://cpp.zjut.edu.cn/ShowProblem.aspx?ShowID=1003

Description:

Given is an alphabet {0, 1, ... , k}(0 <= k <= 9 ). We say that a word of length n over this alphabet is tight if any two neighbour digits in the word do not differ by more than 1. 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.

Sample Input:

4
12
53
587

Sample Output:

100.00000
40.74074
17.38281
0.10130


给定两个数k n

用1~k的数组成一个n个数的序列,如果这个序列每两个相邻的数相差不超过,就记为是tight,求这种序列占总序列的比例

#include
#include
using namespace std;

double a[101][10];
int k,n;
int i,j;//loop
double s;

double d;


int main(){
	while(cin>>k>>n){
		if(k<=1){
			cout<<"100.00000\n";continue;}
		s=0;d=1;
		for(i=0;i<=k;i++)
			a[n][i]=1;
		
		for(j=n-1;j>0;j--)
		for(i=0;i<=k;i++)
		if(i==0)a[j][0]=a[j+1][0]+a[j+1][1];
		else
		if(i==k)a[j][k]=a[j+1][k]+a[j+1][k-1];
		else
			a[j][i]=a[j+1][i-1]+a[j+1][i]+a[j+1][i+1];
		for(i=0;i<=k;i++)
			s+=a[1][i];
		for(i=0;i0;j--){
		for(i=0;i<=k;i++)
			cout<


你可能感兴趣的:(Tight words (dp))