PAT 1091 乙级

PAT 1091 自守数

题目:

解这个题的关键我认为在对n*k*k的取余上,用for循环对n*k*k进行取余,将k与每次取余之后的数进行比较。

如果相等则输出n与n*k*k

否则输出No

#include <iostream>
#include<algorithm>
#include<string>
using namespace std;
bool isZS(int k, int nkk)
{
	int mod=10;
	if(nkk == k)
	{
		return true;
	}
	for(int i=0 ; i<to_string(k).length() ;i++)
	{
		if(nkk % mod==k)
		{
			return true;
		}
		mod=mod*10;
	}
	return false;
}
int main(int argc, char** argv)
{
	int m;
	int k=0;
	cin>>m;
	for(int i=0; i<m ;i++)
	{
		cin>>k;
		int flag=0;
		for(int n=0 ; n<10 ; n++)
		{
			if( isZS( k , n*k*k ) )
			{
				cout<<n<<' '<< n*k*k <<endl;
				flag=1;
				break;
			}
		}
		if(flag==0)
		{
			cout<<"No"<<endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/zouzou_/article/details/86437409