输入一个整数
代表自守数的范围。
输出一个整数,代表
到
之间的自守数个数。
25
5
在这个样例中,
是自守数。
while(True): try: n=input() num=0 for i in range(0,int(n)+1): b=str(i*i) if(str(i)==b[len(b)-len(str(i)):]): num+=1 print(num) except: break不知道endswitch函数,直接这么写也行
while True:
try:
n=int(input())
count=0
for i in range(n+1):
m=i**2
x=len(str(i)) #数字的长度
if int(str(m)[-x:])==i: # m后x位 与i相等
count+=1
print(count)
except:
break
又是比较偷鸡,平方完直接按照原来数据的长度,从最后截取两位来比一下。。。。。
while True: try: n = int(input()) count = 0 for i in range(n+1): lenth = len(str(i)) num = str(i**2) judge = num[len(num)-lenth:len(num)] if judge == str(i): count += 1 print(count) except: break
while True: try: num = int(input()) # 筛出负数 if num >= 0: n = 1 for i in range(num): #先判定是否尾数为1,5,6(尾数为0只考虑0,10、100...等肯定不是); #不是则不再计算和判断,节省一些计算和判断过程 if str(i)[-1] in ['1','5','6']: if str(i*i).endswith(str(i)): n += 1 else: n = 0 print(n) except EOFError: break
# 尾数相同,可以过滤掉很多不用判断的数据 # 尾数为 1 5 6 的数平方尾数才会出现 1 5 6 while True: try: n = int(input()) a, b, c, count = 1, 5, 6, 0 while True: if str(a ** 2).endswith(str(a)): count += 1 if a > n: break a += 10 if str(b ** 2).endswith(str(b)): count += 1 if b > n: break b += 10 if str(c ** 2).endswith(str(c)): count += 1 if c > n: break c += 10 print(count + 1) # 加 1 是因为有个 0 也属于自守数 except: break
def solution(n):
c = 0
d = []
for i in range(n+1):
if i*2 % (10 * (len(str(i)))) == i:
c += 1
d.append(i)
print(c)
while True:
try:
n = int(input())
solution(n)
except:
break