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); } };