W - Given Length and Sum of Digits...

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input
The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.

Output
In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers “-1 -1” (without the quotes).

Examples
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1

#include<stdio.h>
int main()
 {
    
    
     int m,s;
	while(~scanf("%d%d",&m,&s))
	{
    
    
		if(m==1&&s==0)
		printf("0 0\n");
		if((s==0&&m!=1)||m*9<s)
		printf("-1 -1\n");
		if(s<9&&s>0&&m*9>s)
		{
    
    
			if(m==1)
			{
    
    
					printf("%d ",s);
			printf("%d\n",s);
			}
		  else
		  {
    
    
		  		for(int i=1;i<=m;i++)
			{
    
    
				if(i==1)
				printf("1");
				if(i==m)
				printf("%d ",s-1);
				if(i!=1&&i!=m)
				printf("0");
			}
				for(int j=1;j<=m;j++)
			{
    
    
				if(j==1)
				printf("%d",s);
				if(j==m)
				printf("0\n");
				if(j!=1&&j!=m)
				printf("0");
			}
		  }
		
		
		}
		if(s>=9&&m*9>=s)
		{
    
    
				int h=0;
		for(int i=1;i<=m;i++)
		{
    
    
			if(s/9!=0)
			{
    
    
				s=s-9;
				h++;
			}
			else
			break;
		}
		if(s==0)
		{
    
    
			if(m==h)
			{
    
    
				for(int i=1;i<=m;i++)
				printf("9");
				printf(" ");
				
				for(int j=1;j<=m;j++)
				printf("9");
				printf("\n");
			}
			if(m-h==1)
			{
    
    
				printf("18");
				for(int k=1;k<=h-1;k++)
				printf("9");
				printf(" ");
				
				for(int l=1;l<=h;l++)
				printf("9");
				printf("0\n");
			}
			if(m-h>1)
			{
    
    
				printf("1");
				for(int i=1;i<=m-h-1;i++)
				printf("0");
				printf("8");
				for(int j=1;j<=h-1;j++)
				printf("9");
				printf(" ");
				
				for(int i=1;i<=m;i++)
				{
    
    
					if(i<=h)
					printf("9");
					else
					printf("0");
				}
				printf("\n");
			}
		}
		if(s>0)
		{
    
    
			if(h+1==m)
			{
    
    
					for(int i=1;i<=m;i++)
		    	{
    
    
				if(i==1)
				printf("%d",s);
				else
				printf("9");
		    	}
		    	printf(" ");
			
		        	for(int j=1;j<=m;j++)
		    	{
    
    
				if(j==m)
				printf("%d\n",s);
				else
				printf("9");
		    	}
			}
			if(m-h>1)
			{
    
    
				printf("1");
				for(int i=1;i<=m-h-2;i++)
				printf("0");
				printf("%d",s-1);
				for(int i=1;i<=h;i++)
				{
    
    
					printf("9");
					
				}
              printf(" ");
				for(int j=1;j<=h;j++)
					printf("9");
					printf("%d",s);
					for(int i=1;i<=m-h-1;i++)
					printf("0");
					printf("\n");
				
			}
		
		}
		}
	
	}
   return 0;
}

//明白题意后,分情况讨论

猜你喜欢

转载自blog.csdn.net/weixin_51713993/article/details/113759389