要求实现一个函数,统计给定区间内的三位数中至少有两位数字相同的完全平方数(如144、676)的个数。
函数接口定义:
search(n);
其中传入的参数n是一个三位数的正整数。函数search返回[101, n]区间内所有满足条件的数的个数。
裁判测试程序样例:
/* 请在这里填写答案 */
n = int(input())
cnt = search(n)
print ('count={}'.format(cnt))
输入样例:
500
输出样例:
count=6
import math
def search(n):
res_num = []
for num in range(101, n):
x = []
res = math.sqrt(num)
if not res-int(res): # 判断平方根小数点后是否全不为零,
for a in str(num):
x.append(a)
if x[0] == x[1] or x[0] == x[2] or x[1] == x[2]:
res_num.append(num)
return len(res_num)
n = int(input())
cnt = search(n)
print ('count={}'.format(cnt))
这个可以?