1091 N- automorphic number (15 minutes)

If after a certain number of K multiplied by the square of N, the end result is equal to the number of digits K, then call this number "N- number sober." For example, 3 × 92 2 = 25392, and 25392 of the end 92 is just two, so the number 92 is a 3- sober.

This question will ask you to write a program to determine whether a given number on a number N is N- sober.

Input formats:

Input given positive integer M (≦ 20) in the first row, and then the M line to be detected is given, a positive integer of not more than 1,000.

Output formats:

If it is N- Automorphic number for each digital outputs need to detect the smallest value in the row N and NK 2, separated by a space; otherwise the output  No. Note that to ensure that the subject N <10.

Sample input:

3
92 5 233

Sample output:

3 25392
1 25
No

 

#include<iostream>
#include<string>
using namespace std;
int main(){
//	freopen("input.txt","r",stdin);
	int n;
	cin>>n;
	string s_x,s_y;
	for(int i=0;i<n;i++){
		int x, flag=0;
		cin>>x;
		s_x = to_string(x); //int to string
		for(int j=1;j<10;j++){
			int y=j*x*x;
			s_y = to_string(y);
			if(s_x == s_y.substr(s_y.size() - s_x.size(), s_x.size())){
				printf("%d %d",j,y);
				flag = 1;
				break;
			}
		}
		if(flag==0) printf("No");
		if(i!=n-1) printf("\n"); //注意格式 
	}
	
	return 0;
}

 

Published 67 original articles · won praise 14 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_38603360/article/details/103701363