HUST 1600 Lucky Numbers

1600 - Lucky Numbers

时间限制:2秒 内存限制:64兆
401 次提交 109 次通过
题目描述
Isun loves digit 4 and 8 very much. He thinks a number is lucky only if the number satisfy the following conditions:
1. The number only consists of digit 4 and 8.
2. The number multiples 48.
One day, the math teacher gives Isun a problem:
Given L and R(1 <= L <= R <= 10^15), how many lucky numbers are there between L and R. (i.e. how many x satisfy L <= x <= R, x is a lucky number).
输入
Multiple test cases. For each test case, there is only one line consist two numbers L and R.
输出
For each test case, print the number of lucky numbers in one line.

Do use the %lld specifier or cin/ cout stream to read or write 64-bit integers in С++.
样例输入
1 48
1 484848
样例输出
1
7
提示

用深搜把每个符合条件的数字找出来,打表,然后就好了

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
long long int l,r;
long long int ans[200005];
int cot;
int count(int x)
{
    int num=0;
    while(x>0)
    {
        num++;
        x/=10;
    }
    return num;
}
void dfs(long long int x,int cnt)
{
    if(cnt>16)
        return;
    if(x%48==0)
       ans[cot++]=x;
    dfs(x*10+8,cnt+1);
    dfs(x*10+4,cnt+1);
}
int cmp(long long int x,long long int y)
{
    return x<y;
}
int main()
{
    cot=0;
    memset(ans,0,sizeof(ans));
    dfs(4,1);
    dfs(8,1);


    while(scanf("%lld%lld",&l,&r)!=EOF)
    {

         sort(ans,ans+cot,cmp);
         int pos=0;
         for(int i=0;i<cot;i++)
         {
             if(l<=ans[i])
             {
                 pos=i;
                 break;
             }

         }
         int res=0;

         for(int i=pos;i<cot;i++)
         {
              if(ans[i]<=r)
                  res++;
              else
                  break;

         }
         printf("%d\n",res);




    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值