CF1714C 题解

题目大意

  • 找到最小的数字,使该数字每一位上的数字的和等于给定的数字 \(s\),且其中的所有数字都不同,即所有数字都是唯一的。

解法

这题的数据很水,暴力就能过,从小到大枚举每一位上的数字即可。

注意枚举的时候要从比上一次大的位置枚举,不能有重复。

注意边界情况。

代码

#include <bits/stdc++.h>
using namespace std;
int s;
string ans;

void dfs(int sum, string now, int last) {
	if (sum > s) {
		return;
	}

	if (sum == s) {
		if (ans.size() > now.size()) {
			ans = now;
		} else if (ans > now) {
			ans = now;
		}

		return;
	}

	for (int i = last + 1; i < 10; i++) {

		dfs(sum + i, now + to_string(i), i);
	}
}

int main() {
	for (int i = 1; i <= 45; i++) {

		ans = "999999999999999999999999999999999";
		s = i;
		dfs(0, string(), 0);
		cout << s << " " << ans << endl;
	}

	return 0;
}

另附打表做法

#include <bits/stdc++.h>
using namespace std;

int ans[46] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 29, 39, 49, 59, 69, 79, 89, 189, 289, 389, 489, 589, 689, 789, 1789, 2789, 3789, 4789, 5789, 6789, 16789, 26789, 36789, 46789, 56789, 156789, 256789, 356789, 456789, 1456789, 2456789, 3456789, 13456789, 23456789, 123456789};

int main() {
	int t;
	scanf("%d", &t);

	while (t--) {
		int n;
		scanf("%d", &n);
		printf("%d\n", ans[n]);
	}

	return 0;
}

posted @ 2022-08-15 16:28  Dregen_Yor  阅读(110)  评论(0)    收藏  举报