26
#include
#include
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<
}
return 0;
}
编辑于 2016-07-06 15:45:52
回复(17)
14
#include
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
回复(4)
20
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
回复(11)
8
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
回复(1)
7
#include
#include
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<
}
return 0;
}
发表于 2017-08-06 15:34:38
回复(0)
6
//自守数
#include
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)
5
//目前最短的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
回复(4)
4
while 1:
try:
n = int(input())
res = 0
for i in range(n+1):
temp = str(i*i)
l = len(str(i))
if temp[-l:] == str(i):
res += 1
print(res)
except:
break
发表于 2020-02-01 16:41:02
回复(2)
2
//深度优先遍历,从1,5,6开始,在他们是自守数的情况下在头部尝试添加1到9。
//若添加后不构成自守数,则放弃往下dfs,否则,count++,继续向下深搜。
//时间复杂度从O(N)降低到O(log(N))...不晓得这个算对没..
import java.util.*;
public class Main {
public static int count = 1;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n=in.nextInt();
int[] nums = new int[]{1,5,6};
for(int i = 0; i
int cur = nums[i];
dfs(cur, n);
}
System.out.println(count);
count = 1;
}
}
public static void dfs(int cur, int n) {
if(cur > n)
return;
if(check(cur)) {
count++;
for(int j = 1; j
String tmp = String.valueOf(cur);
tmp =j + tmp;
int tmpCur = Integer.parseInt(tmp);
dfs(tmpCur, n);
}
}
}
public static boolean check(int i) {
String a = String.valueOf(i);
int c = i*i;
String b = String.valueOf(c);
int cura = a.length()-1;
int curb = b.length()-1;
while(cura >= 0) {
if(a.charAt(cura) != b.charAt(curb))
return false;
cura--;
curb--;
}
return true;
}
}
发表于 2020-07-28 16:46:42
回复(1)
2
while( n = readline()){
var res = [];
for(var num = 0 ; num <= +n ; num ++){
var len = (num + '').length;
var str = (num + '');
var all = 0;
var istrue = true;
for(var i = 0 ; i
for(var j = 0 ; j <= i ; j ++){
all += str[len-1-j] * str[len-1-(i-j)] * Math.pow(10, i);
}
if((all + '').slice(-i-1) !== str.slice(-i-1)){
istrue = false;
break;
}
}
if(istrue) {
res.push(num);
}
}
console.log(res.length);
} 害怕暴力求解会产生问题,所以想了想办法先把乘法表达式分解,从后向前一位一位的乘,比如9376,先计算最后一个数,应该为6*6取后一位,为6,在计算最后两位6*6+6*7*10+7*6*10=876,也满足,再计算后三位,876 +3*6*10^2 + 7*7*10^2 + 6*3*10^2 = 9376, 也满足,再计算最后四位,9376 + 9*6*10^3 + 3*7*10^3 + 7*3*10^3 + 6*9*10^3 = 159376,至此已经计算了后四位没必要再计算了,而且满足条件,则成立。
发表于 2020-06-09 14:59:54
回复(1)
2
简单方法很多,第一满足只能是5的n次平方或者5的n次平方*3附近,第二,假设这个数为k,(k-1)或者(k+1)必须有n个2和k的n个5对应,第三,n不能小于位数,比如k=15,n是1,就不行,代码如下:
int count=2,k=5,s=1; //0,1,5,6 25 76,376 625 顺便给出两千内自守数
if(n<2)
{
cout<
}
while(k<=n)
{
s<<=1;
if((k-1)%s==0) count++;
if(k+1<=n&&(k+1)%s==0)count++;
if(s>3&&3*k<=n)
{
if((3*k-1)%s==0)
count++;
if(3*k+1<=n&&(3*k+1)%s==0)
count++;
}
k*=5;
}
cout<
发表于 2020-04-15 11:05:42
回复(1)
2
//自己写个判断函数,也很简单的
#include
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;
}
发表于 2019-07-21 21:30:58
回复(1)
2
#include
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
{
if(automor(i)) cnt++;
}
cout<
}
system("pause");
return 0;
}
编辑于 2018-08-05 11:43:39
回复(1)
2
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)
2
我说怎么看不懂题目,原来 252=625,252最后那个2是平方的意思,***
发表于 2016-07-28 10:13:21
回复(2)
1
while True:
try:
n, cnt = int(input()), 0
for i in range(n+1):
x ,y = str(i*i),str(i)
if x[-len(y):] == y:
cnt += 1
print(cnt)
except:
break
发表于 2020-07-17 17:15:33
回复(0)
1
#include
using namespace std;
//直接取余数
int main()
{
int n;
while(cin>>n)
{
int res = 0;
for(int i = 0;i<=n;i++)
{
if(i<1e1)
res = (i%10 == (i*i)%10)?res + 1:res;
else if(i<1e2)
res = (i%100 == (i*i)%100)?res + 1:res;
else if(i<1e3)
res = (i%1000 == (i*i)%1000)?res + 1:res;
else if(i<1e4)
res = (i%10000 == (i*i)%10000)?res + 1:res;
}
cout<
}
return 0;
}
发表于 2020-07-11 09:09:23
回复(0)
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;
for(int i=0;i<=num;i++){
int length = (i+"").length();
if(i*i%(Math.pow(10,length))==i){
count++;
}
}
System.out.println(count);
}
}
}
发表于 2020-07-10 10:59:18
回复(0)
1
#include
using namespace std;
int main(){
int n;
while(cin >> n){
int result=0;
for(int i=0;i <= n;++i){
string sq = to_string(i*i);
string numstr = to_string(i);
if(0==sq.substr(sq.size()-numstr.size()).compare(numstr)) result+=1;
}
cout <
}
return 0;
}
发表于 2020-06-21 22:46:02
回复(0)
1
我的疑问是:自守数是指一个数的平方的尾数等于该数自身的自然数。
0到底是不是自然数!
发表于 2020-03-16 12:18:31
回复(0)