网站建设公司哪家好 地址磐石网络,miit网站备案,企查查个人信息查询,汕头网站开发找哪里Minimum Varied Number
题面翻译
题目描述
找出数码和为 s s s 的最小数字#xff0c;使得其中的所有数字都是不同的#xff08;即所有数字都是唯一的#xff09;。
例如#xff0c;如果 s 20 s20 s20 #xff0c;那么答案是 389 389 389。这是最小的数字#xf…Minimum Varied Number
题面翻译
题目描述
找出数码和为 s s s 的最小数字使得其中的所有数字都是不同的即所有数字都是唯一的。
例如如果 s 20 s20 s20 那么答案是 389 389 389。这是最小的数字其中所有数字都不同数字的总和为 20 20 20 3 8 9 20 38920 38920。
对于给定的 s s s 输出这个最小数字。
输入格式
第一行包含整数 t t t ( 1 ≤ t ≤ 45 1≤t≤45 1≤t≤45 — 测试用例的数量。
每个测试用例由包含一行唯一整数指定的 s s s ( 1 ≤ s ≤ 45 1≤s≤45 1≤s≤45)。
输出格式
输出 t t t 个整数 ― 给定测试用例的答案。
样例解释
对于第一个测试用例 s 20 s20 s20最小数字为 389 389 389。
对于第二个测试用例 s 8 s8 s8最小数字为 8 8 8 8 8 88 88。
对于第一个测试用例 s 45 s45 s45最小数字为 123456789 123456789 123456789 1 2 3 4 5 6 7 8 9 45 12345678945 12345678945。
对于第一个测试用例 s 10 s10 s10最小数字为 19 19 19 1 9 10 1910 1910。
题目描述
Find the minimum number with the given sum of digits $ s $ such that all digits in it are distinct (i.e. all digits are unique).
For example, if $ s20 $ , then the answer is $ 389 $ . This is the minimum number in which all digits are different and the sum of the digits is $ 20 $ ( $ 38920 $ ).
For the given $ s $ print the required number.
输入格式
The first line contains an integer $ t $ ( $ 1 \le t \le 45 $ ) — the number of test cases.
Each test case is specified by a line that contains the only integer $ s $ ( $ 1 \le s \le 45 $ ).
输出格式
Print $ t $ integers — the answers to the given test cases.
样例 #1
样例输入 #1
4
20
8
45
10样例输出 #1
389
8
123456789
19Solution
采用贪心的策略为了使数字最小则必须保证低数位数一定是最大的
首先从9-1开始选取最大的数字若数字之和小于 s s s则放入 n n n中并计算 n n n中的数字和当数字之和n等于s时退出循环返回n数字之和大于 s s s则不进行处理
//
// Created by Gowi on 2023/12/2.
//#include iostream
#include cmathusing namespace std;int main() {int t;cin t;while (t--) {int s, n 0, sum 0, v 0;cin s;for (int i 9; i 0; i--) {if (sum i s) {n i * pow(10, v) n;sum i;v;}if (sum s) {break;}}cout n endl;}return 0;
}