PAT-B 1091 N-automatic number (15 points)


1. Topic

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". For example, 3 × 9 2 2 = 25392 3 \times 92^{2}=253923×922=2 5 3 9 2 , and25392 253922 5 3 9 2 The last two digits are exactly92 929 2. So92 929 2 is a 3-autonomous number

Input format:

Enter a positive integer M (≤20) in the first line, and M positive integers not exceeding 1000 to be detected in the following line.

Output format:

For each number that needs to be detected, if it is an N-autonomous number, output the smallest NN in a rowN andNK 2 NK^{2}NKThe value of 2​​ is separated by a space; otherwise, No is output. Pay attention to the question to ensure that N<10.

Input sample:
3
92 5 233
Output sample:
3 25392
1 25
No


2. Ideas and precautions

(1) How to get the last n digits of a certain number?

For example:
92 has 2 digits, then use 25392%100 to get the last two digits.
Get the last n bits and divide by 1 0 n 10^{n}10n
so we have to get the number of digits in the input first

//...
	int temp=n;
	int count=0;  //获取该数的位数 
	
	while(temp>0)   //获取n的位数 
	{
    
    
		temp/=10;
		count++; 
	}

If the input has n bits, the divisor is

for(int i=0;i<count;i++)
	{
    
    
		v*=10;   //取得除数 
	}

(2) For loop, try out the N-autonomous number N

Note: The
title guarantees that N<10, so we only need to cycle to 10, otherwise the sample 233 will make an error.
(233 is also a self-preserved number, but the return value is not in the range) The
output is "No", do not write "NO" ";

Return the autonomic coefficient j.

for(int j=1;j<10;j++)
	{
    
       
		int test=j*n*n;
		if(test%v==n) 
		{
    
    
		return j;   
		break;	
		}
	}



Three, AC code

#include<iostream>
#include<cstdio>

using namespace std;
int zishou(int n);
int main()
{
    
    
  
  int temp=0;
  int N;
  cin>>N;
	  for(int i=0;i<N;i++)
	  {
    
    	
		  cin>>temp;
		  int k=zishou(temp);
		  
		  	if(k==-1) cout<<"No"<<endl;
		   else cout<<k<<" "<<temp*temp*k<<endl;
	  }
  
}

int zishou(int n)
{
    
    	
		//如果你输入92,那么获得的v为100 
	int v=1;
	int temp=n;   
	int count=0;  //获取该数的位数 
	
	while(temp>0)   //获取n的位数 
	{
    
    
		temp/=10;
		count++; 
	}
		
	for(int i=0;i<count;i++)
	{
    
    
		v*=10;   //取得除数 
	}

	for(int j=1;j<10;j++)
	{
    
       
		int test=j*n*n;
		if(test%v==n) 
		{
    
    
		return j;   
		break;	
		}
	}
    return -1;  //如果该数不是自守数,或者是自守数但是返回的数字不在<10的范围内,就返回一个-1;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/SKMIT/article/details/113823078