HDU 2089 —— 不要62 [入门题]

http://acm.hdu.edu.cn/showproblem.php?pid=2089



#include<stdio.h>
#include<string.h>
int n, m;
int dp[15][15];
int num[15];

void init()
{
	dp[0][0] = 1;
	for(int i = 1;i<=7;i++)
	{
		for(int j = 0;j<=9;j++)
		{
			if(j == 4)	continue;
			for(int k = 0;k<=9;k++)
			{
				if(j == 6 && k == 2)
					continue;
				dp[i][j] = dp[i][j] + dp[i-1][k];
			}
		}
	}
}

int work(int k)
{
	memset(num, 0, sizeof num);
	int cnt = 0;
	while(k)
	{
		num[++cnt] = k % 10;
		k /= 10;
	}
	int ans = 0, i;
	for(i = cnt;i>=1;i--)
	{
		for(int j = 0;j<num[i];j++)
		{
			if(j == 4 || (j == 2 && num[i+1] == 6))	continue;
			ans += dp[i][j];
		}
		if(num[i] == 4 || (num[i+1] == 6 && num[i] == 2))
		break;
	}
	if(i == 0)	ans++;
	return ans;
}

int main()
{
	init();
	while(~scanf("%d%d", &n, &m))
	{
		if(n == 0 && m == 0)
		break;
		int sum = 0;
		n--;
		sum += work(m);
		sum -= work(n);
		printf("%d\n", sum);
	}
	return 0;
}


你可能感兴趣的:(HDU 2089 —— 不要62 [入门题])