Description
windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,
在A和B之间,包括A和B,总共有多少个windy数?
Input
包含两个整数,A B。
Output
一个整数
Sample Input
【输入样例一】
1 10
【输入样例二】
25 50
1 10
【输入样例二】
25 50
Sample Output
【输出样例一】
9
【输出样例二】
20
9
【输出样例二】
20
HINT
【数据规模和约定】
100%的数据,满足 1 <= A <= B <= 2000000000 。
数位DP模板题
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 int dp[15][15],a[15],l,r; 6 int Dfs(int pos,int pre,bool zero,bool limit)//(当前位置,高一位的数是什么,是否含有前导零,当前位枚举时是否有上限限制) 7 { 8 if (pos==0) return 1; 9 if (!limit && !zero && dp[pos][pre]) return dp[pos][pre]; 10 int up=limit?a[pos]:9; 11 12 int ans=0; 13 for (int i=0;i<=up;++i) 14 if (pre==10 || zero || i<=pre-2 || i>=pre+2) 15 ans+=Dfs(pos-1,i,zero && i==0,limit && i==a[pos]); 16 if (!limit && !zero) dp[pos][pre]=ans; 17 return ans; 18 } 19 20 int Solve(int x) 21 { 22 int pos=0; 23 while (x) 24 a[++pos]=x%10,x/=10; 25 return Dfs(pos,10,true,true); 26 } 27 28 int main() 29 { 30 scanf("%d%d",&l,&r); 31 printf("%d",Solve(r)-Solve(l-1)); 32 }