[编程题]自守数

/*

int n

n以内自守数的数量。
*/

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

return 0;
}

`int型整数`

##### 输出描述:
`n以内自守数的数量。`

`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;
}```

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())))
``````

``````print(len(list(filter(lambda c:str(c**2).endswith(str(c)),range(int(input()))))))
``````

//自守数
#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);

}
`}`

```#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;
}```

```//目前最短的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();
}
}```

``````#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;
}
``````

```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();
}
}```

```while True:
try:
n=int(raw_input())
if n==1:
print(1)
else:
x=0
count=0
while x<n:
m=x*x
str1=str(m)
str2=str(x)
l=len(str1)-len(str2)
if str2==str1[l:]:
count+=1
x=x+1
#count=count+1
print(count)
except:
break```

```//自己写个判断函数，也很简单的
#include<iostream>
using namespace std;
bool isZiShouShu(int num){//判断是否为自守数
long long squrtNum = num*num;
while (squrtNum && num){
if (squrtNum % 10 != num % 10){
return false;
}
else{
squrtNum /= 10;
num /= 10;
}
}
return true;
}
int main(){
int n;
while (cin >> n){
int cnt = 0;
for (int i = 0; i <= n; i++){
if (isZiShouShu(i))cnt++;
}
cout << cnt << endl;
}
return 0;
}
```

```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);  }  }
}
```

```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
```

```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);
}
}
}```

```//我的思路是先计算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;
}
}```

```#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;
} ```

# 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;
``````

}

#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;
}

```#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;
}```

```#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;
}
}```

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;
}

`}`

177条回答 19593浏览

# 通过挑战的用户

• 2019-10-23 00:18:34
• 2019-10-22 21:55:35
• 2019-10-22 21:26:39
• 2019-10-22 18:13:38
• 2019-10-22 15:29:23

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题