JZ高中OJ 1039. [SCOI2009]windy数

Description
windy定义了一种windy数。
不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。
windy想知道,在A和B之间,包括A和B,总共有多少个windy数?
 
Input
两个整数,A B。
Output
一个整数,表示A~B中有多少个windy数。
 
Sample Input
1 10
Sample Output
9
 
Data Constraint
 
 
Hint
100%的数据,满足 1 <= A <= B <= 2000000000 。
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int num[11],dp[11][11];
 4 inline int read()
 5 {
 6   char ch=getchar();int num=0;
 7   while(ch<'0'||ch>'9')ch=getchar();
 8   while(ch>='0'&&ch<='9'){
 9     num=num*10+ch-'0';ch=getchar();}
10   return num;
11 }
12 inline int Abs(int x)
13 {
14   return x>0?x:-x;
15 }
16 inline int dfs(int len,int las,bool top,bool zero)
17 {
18   if(len==0)return 1;
19   if(!top&&!zero&&dp[len][las]!=-1)
20     return dp[len][las];
21   int cnt=0,maxx=(top?num[len]:9);
22   for(int i=0;i<=maxx;i++){
23     if(Abs(i-las)<2)continue;
24     int p=i;if(zero&&i==0)p=-555;
25     cnt+=dfs(len-1,p,top&&(i==maxx),(p==-555));
26   }
27   if(!zero&&!top)
28       dp[len][las]=cnt;
29   return cnt;
30 }
31 inline int work(int x)
32 {
33   int tot=0;
34   while(x){
35       num[++tot]=x%10;
36     x/=10;
37     }
38   memset(dp,-1,sizeof(dp));
39   return dfs(tot,-555,true,true);
40 }
41 int main()
42 {
43   int x=read();
44   int y=read();
45   printf("%d",work(y)-work(x-1));
46   return 0;
47 }

转载于:https://www.cnblogs.com/anbujingying/p/11329231.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值