Z - Olesya and Rodion(找一个由n个数组成能被t除尽的数)

Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. Find some number that satisfies both of them.

Your task is: given the n and t print an integer strictly larger than zero consisting of n digits that is divisible by t. If such number doesn’t exist, print  - 1.

Input
The single line contains two numbers, n and t (1 ≤ n ≤ 100, 2 ≤ t ≤ 10) — the length of the number and the number it should be divisible by.

Output
Print one such positive number without leading zeroes, — the answer to the problem, or  - 1, if such number doesn’t exist. If there are multiple possible answers, you are allowed to print any of them.

Examples
Input
3 2
Output
712
正确代码:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    
    
	int n,t;
	cin>>n>>t;
	if(n==1)//特殊情况
	{
    
    
		if(t==10)
		cout<<"-1"<<endl;
		else
		cout<<t<<endl;
	}
	else
	{
    
    
		if(t==10)
		{
    
    
			cout<<"1";
		for(int i=1;i<=n-1;i++)
		cout<<"0";
		}
		else
		{
    
    
			for(int i=1;i<=n;i++)//输出n个t
			cout<<t;
		}
		
	}
	return 0;
}

错误代码:
//对于题上所给的简单例子来说,这样写思路是没问题,但是啊,我这样在n越来越大时,会使数据变得巨大,例如:n=18.那sum,min什么的就比较大了,而上一个代码只需要简单地判断一下就可以输出了

#include <iostream>
using namespace std;
int main()
{
    
    
    int n,t;
    cin>>n>>t;
    int sum=1;
    for(int i=1;i<=n;i++)
    {
    
    
    	sum=sum*10;
	}
	int min=1;
	for(int i=1;i<=n-1;i++)
	   min=min*10;
	   int k,f=0;
    for(int i=min;i<=sum-1;i++)
    {
    
    
    	if(i%t==0)
    	{
    
    
    		f=1;
    		k=i;
    		break;
		}
	}
	if(f==1)
	cout<<k<<endl;
	else
	cout<<"-1"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51713993/article/details/113839233
z