首页 > 试题广场 > 自守数
[编程题]自守数

自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n以内的自守数的个数


接口说明


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


输入参数:
int n

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


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

return 0;
}




输入描述:

int型整数



输出描述:

n以内自守数的数量。

示例1

输入

2000

输出

8
#include <iostream>
#include <string>
using namespace std;
//转换成字符串 查找,利用现成的库函数 =-=
int main()
{
	long n;
	while(cin>>n){
		int ans = 2;// 0, 1也是
		for(long i = 3; i<=n; i++){
			long n2 = i*i;
			string s1 = to_string(i);
			string s2 = to_string(n2);
			int pos = s2.size()- s1.size();
			if(s2.find(s1,pos) != -1)
				ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

编辑于 2016-07-06 15:45:52 回复(8)

python 常规的解法:

while True:
    try:
        a, res = int(input()), 0
        for i in range(0, a + 1):
            if str(i ** 2).endswith(str(i)):
                res += 1
        print(res)
    except:
        break

使用函数式编程:

print len(filter(lambda x: str(x ** 2).endswith(str(x)), range(input())))

上面这是python 2.7的写法,在python3中,这样写会报错,要先将filter转为list:

print(len(list(filter(lambda c:str(c**2).endswith(str(c)),range(int(input()))))))
编辑于 2018-09-29 18:14:37 回复(3)
//自守数
#include <stdio.h>
int main()
{
int n;
int num=0;
int count = 0;
while(scanf("%d", &n)!=EOF)
    {
        num = 0;
        count = 0;
        while (num <= n)
        {
            int num2 = num*num;
            int i = num;
            while (i > 0)
            {
                if ((i % 10) == (num2 % 10))
                {
                    i = i / 10;
                    num2 = num2 / 10;
                }
                else
                    break;
            }
            if (i == 0)
                count++;
            num++;
        }
        printf("%d\n", count);

    }
}

发表于 2016-03-20 20:17:53 回复(1)
#include <iostream>
#include <string>
using namespace std;

int main(){
    int n;
    while(cin>>n){
        int cnt=0;
        string pow;
        string str;
        for(int val=0;val<=n;++val){
            pow=to_string(val*val);
            str=to_string(val);
           	if(pow.substr(pow.size()-str.size())==str)
                ++cnt;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

发表于 2017-08-06 15:34:38 回复(0)
//目前最短的java代码?
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int N = in.nextInt();
           int cnt = 0;
		   for(int i=0;i<=N;i++){
			   if(String.valueOf(i*i).substring(String.valueOf(i*i).length()-String.valueOf(i).length()).equals(String.valueOf(i)))
				   cnt++;
		   }
		   System.out.println(cnt);
        }
        in.close();
    }
}

发表于 2017-07-10 11:58:44 回复(0)
#include  <bits/stdc++.h>
using namespace std;
bool automor(int x)
{
    int y=x*x;
    while(x)
    {
        if(x%10==y%10)
        {
            x/=10;
            y/=10;
        }
        else break;
    }
    if(x==0) return true;
    else return  false;

}
int main()
{
    int n;
    while(cin>>n)
    {
         bool res;
         int cnt=0;
         for(int i=0;i<n;i++)
         {
             if(automor(i)) cnt++;
         }
        cout<<cnt<<endl;
    }

    system("pause");
    return 0;
}
编辑于 2018-08-05 11:43:39 回复(1)
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int num = sc.nextInt();
            int count = 0;
            while(num >= 0){
            	long num_square = num * num;
            	char[] c1 = String.valueOf(num_square).toCharArray();
            	char[] c2 = String.valueOf(num).toCharArray();
            	int c1_length = c1.length;
            	int c2_length = c2.length;
            	int i;
            	for(i = 1; i <= c2_length; i++){
            		if(c1[c1_length - i] != c2[c2_length - i])
            			break;
            	}
            	if(i == c2_length + 1)
            		count++;
            	num--;
            }
            System.out.println(count);
        }
        sc.close();
    }
}

发表于 2016-08-11 15:52:23 回复(0)
我说怎么看不懂题目,原来 252=625,252最后那个2是平方的意思,艹
发表于 2016-07-28 10:13:21 回复(2)
import java.util.Scanner;
public class Main{     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         while (sc.hasNext()) {             int n = sc.nextInt();             int result = 0;             for (int i = 0; i < n; i++)if(String.valueOf(i*i).substring(String.valueOf(i*i).length()-String.valueOf(i).length(), String.valueOf(i*i).length()).equals(String.valueOf(i)))result++;             System.out.println(result);         }     }
}
转成String
用substring方法截取与原数同长的子string
进行比较

发表于 2019-06-24 15:57:40 回复(0)
while True:
    try:
        n=int(input())
        count=0
        for i in range(n):
            squre=str(i*i)
            iStr=str(i)
            if iStr==squre[len(squre)-len(iStr):]:
                count+=1
        print(count)
    except:
        break

发表于 2019-04-22 19:01:37 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int cnt = 0;
            for(int i=0; i<=n; i++) {
                long end = i*i;
                if(String.valueOf(end).endsWith(String.valueOf(i))) {
                    cnt ++;
                }
            }
            System.out.println(cnt);
        }
    }
}

发表于 2018-10-09 19:33:27 回复(0)
//我的思路是先计算n的位数,然后从最低位开始判断n与n^2的每一位是否相等
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int cnt = 0, n = sc.nextInt();
            for(int i = 0; i <= n; i++) {
                if(CalAutomorphicNumbers(i)) cnt++;
            }
            System.out.println(cnt);
        }    
    }

    public static boolean CalAutomorphicNumbers(int n) {
        int flag = 1, digit = 0, temp = n, square = n * n;
        while(temp > 0) {
            temp /= 10;
            digit++;
        }
        while(digit > 0) {
            if(square % 10 != n % 10) flag = 0;
            square /= 10;
            n /= 10;
            digit--;
        }        
        if(flag == 1) return true;        
        return false;
    } 
}
编辑于 2018-09-18 20:59:32 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    while(cin>>n) {
        int cnt=0;
        for(int i=0; i<=n; i++) {
            int j=i,m=1;
            while(j) {
                j/=10;
                m*=10;
            }
            if(i*(i-1)%m==0)
                cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
} 

发表于 2017-11-22 16:44:49 回复(1)

include

include

using namespace std;

int main()

{

int n;

while (cin>>n)

    {

    int m,l,num=0;

    for (int i=0;i<n;++i)

        {

        l=pow(i,2);

        for (int j=0;j<7;++j)

            {

            m=pow(10,j);

            if (l%m==i)

                {

                num++;

                break;

            }

        }

    }

  cout<<num<<endl;

}

return 0;

}

发表于 2017-09-01 14:59:12 回复(0)
#include <iostream>
#include <math.h>
using namespace std;

int
main(){
    int n;
    int nsquare;
    while (cin >> n){
        int count = 0;
        
        
        for (int i = 0; i <= n; i++){
            int Numcount = 0;
            int tmp = i;
            while (tmp){
                tmp /= 10;
                Numcount++;
            }
            //cout << Numcount << endl;
            nsquare = i * i;
            if ((nsquare % (long)pow(10, Numcount)) == i) count++;
        }
        cout << count << endl;
    }
    return 0;
}
发表于 2017-08-22 00:17:24 回复(0)
#include<iostream>
using namespace std;
int main() {
	int n;
	while (cin >> n) {
		int base = 10, count = 0;
		for (int i = 0; i <= n; i++) {
			if (i == base) base *= 10;
			if (i*i%base == i)
				count++;
		}
		cout << count << endl;
	}
	return 0;
}

发表于 2017-08-03 22:55:53 回复(0)
#include <iostream>

using namespace std;

int zishoushuNum(int n)
{
    if(n == 0)
        return 1;
    if(n == 1)
        return 2; 
    unsigned int res = 2;
    int temp;
    for(int i= 2; i <= n;i++ )
    {
        temp = i;
        int t10 = 1;
        while(temp)
        {
            t10  *= 10;
            temp /= 10;
        }
        if( (i*i-i)%t10 == 0 )
           res++;
    }
    return  res;
}

int main()
{
    int n;
    while(cin >> n)
    {
        cout << zishoushuNum(n) << endl;
    }
}

编辑于 2017-07-30 21:32:29 回复(0)
import java.util.*;

public class Main {  
 
    public static void main(String[] args) {   
        Scanner scan = new Scanner(System.in); 
        while(scan.hasNext()){
        int ans = CalcAutomorphicNumbers(scan.nextInt());
        System.out.println(ans);
        }
    }
    
    public static int CalcAutomorphicNumbers(int n){
    int times = 0;
    for(int i = 0; i <= n; i++){
    String num = Integer.toString(i);
    String square = Integer.toString(i*i);
    int len = num.length();
   
    if(square.substring(square.length()-len).equalsIgnoreCase(num))
    times++;
    }
   
    return times;
    }
    
}

发表于 2017-07-23 16:56:25 回复(0)
java的String有一个比较方法,str1.ensWith(String str2);应该好处理了
发表于 2017-05-08 10:47:07 回复(0)
#include<stdio.h>
#include<math.h>
int main()
{
int N,M=0,i,j=0,k,a,b;
while(scanf("%d",&N)!=EOF)
{
M=0;
for(i=0;i<=N;i++)
{
a=pow(i,2);
b=i;
for(j=0;b!=0;j++)
{
b=b/10;
}

if(a%(int)pow(10,j)==i)
{
M++;
}
}
printf("%d\n",M);

}
}

发表于 2017-04-10 21:11:26 回复(0)