C. Unique Number(思维)

题目

思路:其实贪心一下,我们要想数字越小,则在后面的位的数字应该越大,这样留给前面的位的数字才会越小,使得数字总值越小,因此我们从后往前填9 8 7 6…直到填到值大于总和的值然后填入还剩下的数补上即可,并可以发现最大数字值为45 ,123456789,每个数字不同。细节见代码。

Code

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#include<cmath>
#include<bitset>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 1e6 + 5;
int lst[Max];
int Mod = 1e9 + 7;


int main()
{
    
    
	FAST;
	int t;cin >> t;
	while (t--)
	{
    
    
		int n;cin >> n;
		int k = 0;
		int g = 9;
		if (n > 45)cout << "-1" << endl;
		else
		{
    
    
			while (n > g)
			{
    
    
				lst[++k] = g;
				n -= g;
				g--;
			}
			lst[++k] = n;
			for (int i = k;i >= 1;i--)cout << lst[i];
			cout << endl;
		}	
	}
}


猜你喜欢

转载自blog.csdn.net/asbbv/article/details/111244123