`
whxnwjq
  • 浏览: 3696 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

SRM403div2题解

阅读更多

250:水题,就是给定一个n求一个不大于n的数且数字的每一位不是4就是7  暴力即可

 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;

bool istrue(int x) {
    while (x) {
        if (x % 10 == 4 || x % 10 == 7) {
           	x /= 10;
            continue;
        }
        else return false;
    }
    return true;
}

class TheLargestLuckyNumber{
    public:
        int find(int n) {
            while (n) {
                if (istrue(n)) return n;
                n--;
            }
        }
};

500:此题是250题的扩展,问a和b间满足每一位数不是4就是7的数的个数,暴力会超时,没想到什么好的办法,看了别人的代码,觉得那个递归求解很巧妙

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define LL long long

//递归求解
int solve(LL tmp, LL a, LL b) {
    if (tmp > b) return 0;
    return solve(tmp*10+4,a,b)+solve(tmp*10+7,a,b)+(tmp>=a&&tmp<=b);
}

class TheLuckyNumbers {
    public:
        int count(int a, int b) {
            if (a > b) swap(a, b);
            return solve(0, a, b);                   
        }
};
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics