全部位由 4 或 7 构成的数为一度脏数 eg:4,7,44,47,77,444 ...
如果一个数为两个一度脏数相加的和,那么这个数脏度为 2,4 + 7 = 11,11 脏度为 2
给定一个数(N < 1000000),输出它的脏度
输入:40
输出:7
#include <iostream>
#include <vector>
using namespace std;
vector<int>tmp;
int sec = 0;
void TEST() {
tmp.push_back(4);
tmp.push_back(7);
int start = 0;
int end = tmp.size();
while (tmp.back() <= 1000000) {
for (; start < end; start++) {
tmp.push_back(tmp[start] * 10 + 4);
tmp.push_back(tmp[start] * 10 + 7);
}
start = end;
end = tmp.size();
}
}
int dfs(int N, int count) {
if ((N > 4 && N < 7) || N < 4)
return 0;
if (N == 0)
return count;
for (int i = sec; i >= 0; i--) {
int t = dfs(N - tmp[i], count + 1);
if (t != 0) {
return t;
break;
}
}
}
int main() {
int N;
TEST();
while (cin >> N) {
bool is = false;
int i = 0;
for (; tmp[i] <= N; i++) {
if (tmp[i] == N) {
cout << 1 << endl;
is = true;
}
}
if (!is) {
sec = i - 1;
cout << dfs(N, 0) << endl;
}
}
return 0;
}