Unique Number

                                                                                                                       C. Unique Number

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a positive number 𝑥x. Find the smallest positive integer number that has the sum of digits equal to 𝑥x and all digits are distinct (unique).

Input

The first line contains a single positive integer 𝑡t (1≤𝑡≤501≤t≤50) — the number of test cases in the test. Then 𝑡t test cases follow.

Each test case consists of a single integer number 𝑥x (1≤𝑥≤501≤x≤50).

Output

Output 𝑡t answers to the test cases:

 

  • if a positive integer number with the sum of digits equal to 𝑥x and all digits are different exists, print the smallest such number;
  • otherwise print -1.

 

Example

input

Copy

4
1
5
15
50

output

Copy

1
5
69
-1

简单的 一层嵌套可以不写

然而多层的嵌套必须要写,不然滑铁卢

以下分别是写嵌套和不写嵌套的结果:

include <iostream>
using namespace std;
int t, x, i;
 
 
int main()
{
    cin >> t;
    while( t-- )
    {
        cin >> x;
 
        if ( x <= 9)
            printf("%d\n", x);
 
        else if (x >= 10 && x <= 17)
        {
            for(i = 1; i < 9; ++i)
            {
                if (i + 9 == x)
                    cout << i * 10 + 9 <<  endl;
            }
 
        }
            
                    
        else if (x >= 18 && x <= 24)
        {
            for(i = 1; i < 8; ++i)
            {
                if (i + 8 + 9 == x)
                    cout << i * 100 + 89 <<  endl;
            }
        }
        else if (x >= 25 && x <= 30){
            for(i = 1; i < 7; ++i)
            {
                if (i + 7 + 8 + 9 == x)
                    cout << i * 1000 + 789 <<  endl;
            }
        }
        else if (x >= 31 && x <= 35)
        {
            for(i = 1; i < 6; ++i)
            {
                if (i + 6 + 7 + 8 + 9 == x)
                    cout << i * 10000 + 6789 <<  endl;
            }
        }
 
        else if (x >= 36 && x <= 39)
        {
            for(i = 1; i < 5; ++i)
            {
                if (i + 5 + 6 + 7 + 8 + 9 == x)
                    cout << i * 100000 + 56789 <<  endl;
            }
        }
 
        else if (x >= 40 && x <= 42)
        {
            for(i = 1; i < 4; ++i)
            {
                if (i + 4 + 5 + 6 + 7 + 8 + 9 == x)
                    cout << i * 1000000 + 456789 <<  endl;
            }
        }
        else if (x >= 43 && x <= 44)
        {
            for(i = 1; i < 3; ++i)
            {
                if(i + 3 + 4 + 5 + 6 + 7 + 8 + 9 == x)
                    cout << i * 10000000 + 3456789 <<  endl;
            }
        }
 
        else if (x == 45)
        {
            cout<<123456789<<'\n';
        }
 
        else {cout << "-1" << endl;}
 
    }
 
    return 0;
}

 

不写嵌套:

include <iostream>
using namespace std;
int t, x, i;
 
 
int main()
{
    cin >> t;
    while( t-- )
    {
        cin >> x;
 
        if ( x <= 9)
            printf("%d\n", x);
 
        else if (x >= 10 && x <= 17)
        {
            for(i = 1; i < 9; ++i)
            {
                if (i + 9 == x)
                    cout << i * 10 + 9 <<  endl;
            }
 
        }
            
                    
        else if (x >= 18 && x <= 24)
        {
            for(i = 1; i < 8; ++i)
            {
                if (i + 8 + 9 == x)
                    cout << i * 100 + 89 <<  endl;
            }
        }
        else if (x >= 25 && x <= 30){
            for(i = 1; i < 7; ++i)
            {
                if (i + 7 + 8 + 9 == x)
                    cout << i * 1000 + 789 <<  endl;
            }
        }
        else if (x >= 31 && x <= 35)
        {
            for(i = 1; i < 6; ++i)
            {
                if (i + 6 + 7 + 8 + 9 == x)
                    cout << i * 10000 + 6789 <<  endl;
            }
        }
 
        else if (x >= 36 && x <= 39)
        {
            for(i = 1; i < 5; ++i)
            {
                if (i + 5 + 6 + 7 + 8 + 9 == x)
                    cout << i * 100000 + 56789 <<  endl;
            }
        }
 
        else if (x >= 40 && x <= 42)
        {
            for(i = 1; i < 4; ++i)
            {
                if (i + 4 + 5 + 6 + 7 + 8 + 9 == x)
                    cout << i * 1000000 + 456789 <<  endl;
            }
        }
        else if (x >= 43 && x <= 44)
        {
            for(i = 1; i < 3; ++i)
            {
                if(i + 3 + 4 + 5 + 6 + 7 + 8 + 9 == x)
                    cout << i * 10000000 + 3456789 <<  endl;
            }
        }
 
        else if (x == 45)
        {
            cout<<123456789<<'\n';
        }
 
        else {cout << "-1" << endl;}
 
    }
 
    return 0;
}

end!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值