ZOJ3436 July Number 【DFS】


July Number

Time Limit: 2 Seconds      Memory Limit: 65536 KB

The digital difference of a positive number is constituted by the difference between each two neighboring digits (with the leading zeros omitted). For example the digital difference of 1135 is 022 = 22. The repeated digital difference, or differential root, can be obtained by caculating the digital difference until a single-digit number is reached. A number whose differential root is 7 is also called July Number. Your job is to tell how many July Numbers are there lying in the given interval [a,b].

Input

There are multiple cases. Each case contains two integers a and b. 1 ≤ab ≤ 109.

Output

One integer k, the number of July Numbers.

Sample Input

1 10

Sample Output

1


题意:(1)题意:一个数,前后两个数之差组成新数,一直下去,如果结果是7,则为7月数。如7 18 29 70 81 92 108 118 188 198 209 219 229 299 700770 780 790 801 811 881 891 902 910 912 922 980 992
这些都是7月数。给一个区间,问这个区间中有几个7月数。     题目不难,但代码实现有些麻烦。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> ans,v;
int p[11]={0,1,10,100,1000,10000,100000,1000000,10000000,100000000};
void dfs(int len,int i,int x,int cut)
{
    cut=cut*10+i;
    if(len==0)
    {
        v.push_back(cut);
        return ;
    }
    int tem=x/p[len];
    x=x-tem*p[len];
    if(i+tem<=9)dfs(len-1,i+tem,x,cut);
    if(i-tem>=0)dfs(len-1,i-tem,x,cut);
}
void slove()
{
    ans.push_back(7);
    for(int len=1;len<9;len++)
    {
        v.clear();
        for(int i=1;i<=9;i++)
        {
            for(int j=0;j<ans.size();j++)
            {//cout<<"  ***";
                dfs(len,i,ans[j],0);
            }
        }

        ans.insert(ans.end(),v.begin(),v.end());
        sort(ans.begin(),ans.end());
        ans.erase(unique(ans.begin(),ans.end()),ans.end());
    }
}
int main()
{
    slove();
    int x,y;
    while(scanf("%d%d",&x,&y)!=EOF)
    {
        printf("%d\n",upper_bound(ans.begin(),ans.end(),y)-lower_bound(ans.begin(),ans.end(),x));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值