PAT Grade B-N-autonomous number

Title description
If the square of a number K is multiplied by N, and the number of digits at the end of the result is equal to K, then this number is called "N-autonomous number".

E.g. × 92. 3 2 = 25392, and 25392 of the end 92 is just two, so the number 92 is a 3- sober.

This question asks you to write a program to judge whether a given number is an N-autonomous number with respect to a certain N.

Input format The
input gives a positive integer M in the first line, and the
following line gives M positive integers to be detected and no more than 1000.

Output format
, if it is N- Automorphic number for each digital outputs to be detected in the row N and a minimum of NK 2 values, separated by a space; otherwise the output No.

Input example
3
92 5 233

Sample output
3 25392
1 25
No

Data range
N <10, M <20


answer:

#include <iostream>
using namespace std;

bool check(int N, int K)
{
    
    
	int d = 1;
	while(d < 1e6)
	{
    
    
		if(N * K * K % d == K) return true;
		d *= 10;
	}
	return false;
}

int main()
{
    
    
	int K, M;
	cin >> M;
	
	while(M --)
	{
    
    
		cin >> K;
		
		bool flag = false;
		for (int N = 1; N < 10; N ++)
			if(check(N, K)) 
			{
    
    
				flag = true;
				cout << N << ' ' << N * K * K << endl;
				break;
			}
		
		if(!flag) cout << "No" << endl;	
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/113867259