华为OJ(自守数)

描述

自守数是指一个数的平方的尾数等于该数自身的自然数。例如:252 = 625,762 = 5776,93762 = 87909376。请求出n以内的自守数的个数

 

接口说明


 /*
 功能: 求出n以内的自守数的个数


 输入参数:
 int n

 返回值:
     n以内自守数的数量。
 */

 

 public static int CalcAutomorphicNumbers( int n)
 {
     /*在这里实现功能*/

     return 0;
 }

 

 

知识点 查找
运行时间限制 10M
内存限制 128
输入

int型整数

输出

n以内自守数的数量。

样例输入 2000
样例输出 8
循环判断末位对10求余是否相等即可

#include<iostream>
using namespace std;
bool isAutomorphicNumber(int m);
int main()
{	
	int m,cnt=0;
	cin>>m;
	for(int i=0;i<m;i++)
		if(isAutomorphicNumber(i))
			cnt++;
	cout<<cnt<<endl;
	//system("pause");
	return 0;
}
bool isAutomorphicNumber(int m)
{
	
	if(m==0)
		return true;
	else
	{
		int sq=m*m;
		while(m!=0)
		{
			if(m%10!=sq%10)
				return false;
			m/=10;
			sq/=10;
		}
		return true;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值