谕神的密码
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
谕神是集训队里面智商数一数二的大神,在他的电脑里储存着他在学习的东西。每天都要通过学习来提高自己的知识水平。然而,作为一个长者,谕神不愿意将自己的学习资料给那些too young, too simple, sometimes naive的人看,以免被他们看了去搞一个大新闻。于是,谕神给自己的电脑设置了密码。
当然,他设置的密码也要按照基本法。这个基本法是谕神自己搞出来的。法则是这样的:
1、密码由 2 个 n 位数组成,中间由空格隔开,其中,两个数的各个位的和均为 s
2、是满足1条件的最小的数和最大的数
3、当找不到
n
位数的和为s的时候,密码就是:-1 -1
。
4、谕神保证密码的 2 个数中没有前导 0 。
他这个密码只是为了防止too young, too simple, sometimes naive的人进入电脑的,对于你们这些高智商的人群,破译密码应该并不难吧~~
注意,单独的 0 ,是合法的哦~
Input
先读入一个 T ( 0≤T≤100 ),为数据组数
每组数据只有一行,为 n ( 0<n≤100 )和 s ( 0≤s≤900 ).
Output
输出谕神的密码
Sample input and output
Sample Input | Sample Output |
---|---|
1 2 15 | 69 96 |
Hint
就一组测试数据 2 位数各位和为 15 的最大值是 96 , 最小值是 69 .
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define maxn 110
using namespace std;
int rr[maxn],rm[maxn];
void solve(int i,int j,int n,int s)
{
if(i==j)
{
rm[i]=s;
return ;
}
if(s/n>=5)
{
rm[j]=9;
solve(i,j-1,n-1,s-9);
}
else
{
if(i==1)
{
rm[i]=1;
solve(i+1,j,n-1,s-1);
}
else
{
rm[i]=0;
solve(i+1,j,n-1,s);
}
}
}
int main()
{
int t,n,s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&s);
memset(rr,0,sizeof(rr));
memset(rm,0,sizeof(rm));
if(n==1&&s==0)
{
printf("0 0\n");
continue;
}
if(n*9<s||n>1&&s<1)//这里判断写成了s<=1没考虑全面
{
printf("-1 -1\n");
continue;
}
int i=1,j=n,tem=s;
solve(1,n,n,tem);
i=1;
while(s)
{
if(s>=9)
{
rr[i++]=9;
s=s-9;
}
else
{
rr[i++]=s;
s=0;
}
}
for(i=1;i<=n;++i)
printf("%d",rm[i]);
printf(" ");
for(i=1;i<=n;++i)
printf("%d",rr[i]);
printf("\n");
}
return 0;
}