题意:给出一个区间,求这个区间里面的数字一共有多少个数字其中不含数字4并且没有连续的62 。
例如,4,14,412,162等等数字就不满足要求。
分析:
网上都说这题是数位dp,好像是的。可是我还没有入门数位dp,就是用的普通的dp来写的。
dp[i]表示的是在区间(1,i)之间满足要求的数字的个数,递推公式为:dp[i] = dp[i-1]+0/1 .
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int const maxn = 1000005 ;
int dp[maxn];
//dp[i] 表示的是从1到i之间的数的个数
bool Judge(int n)
{
while(n)
{
if(n%10==4||n%100==62)return false;
n/=10;
}
return true ;
}
void cal()
{
memset(dp,0,sizeof(dp));
dp[1] = 1 ;
for(int i = 2 ; i < maxn ; i++)
{
if(Judge(i))dp[i] = dp[i-1]+1 ;
else dp[i] = dp[i-1] ;
}
}
int main()
{
cal();
int n,m;
while(scanf("%d%d",&n,&m)&&n>0&&m>0)
{
printf("%d\n",dp[m]-dp[n-1]);
}
return 0;
}