脏数

全部位由 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值