其他分享
首页 > 其他分享> > PAT 乙级 1091 N-自守数 (15 分)

PAT 乙级 1091 N-自守数 (15 分)

作者:互联网

题目:https://pintia.cn/problem-sets/994805260223102976/problems/1071785664454127616

经验总结:
以逐步取模的方式,判断两个数尾部一不一样。

C++代码:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
	int m;
	cin>>m;
	while(m--){
		int k,n=0,nkk;
		cin>>k;
		for(int i=1;i<10;i++){
			nkk = i*k*k;
			int k1 = k;
			bool flag = true;
			while(k1>0){ //以逐步取模的方式,判断两个数尾部一不一样。
				if(nkk%10 != k1%10){
					flag = false;
					break;
				}
				nkk /= 10;
				k1 /= 10;
			}
			if(flag){
				n = i;
				nkk = i*k*k;
				break;
			}
		}
		if(n>0){
			cout<<n<<" "<<nkk<<endl;
		}else{
			cout<<"No"<<endl;
		}
	}
	return 0;
}

标签:10,15,取模,int,nkk,自守数,flag,PAT,include
来源: https://blog.csdn.net/m0_38088647/article/details/99471318