案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)
对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。
21
2336
一行,多了我不要
try: while True: print(sum(map(lambda x:x**2,filter(lambda x:x%7!=0 and str(x).find('7')==-1, range(1,int(input())+1))))) except Exception: pass
python solution:
while True: try: res = 0 for i in range(1, int(input()) + 1): if "7" not in str(i) and i % 7 != 0: res += i ** 2 print(res) except: break
#tip1: 1^2+2^2+3^2+……+n^2=n(n+1)(2n+1)/6 #tip2: Python很容易实现某字符在字符串中:'7' in str(i) while 1: try: n=int(input()) s=n*(n+1)*(2*n+1)//6 for i in range(1,n+1): if i%7 == 0 or '7' in str(i): s -= i*i print(s) except: break
try: while 1: print sum(map(lambda y:y ** 2,filter(lambda x:not '7' in str(x) and x % 7 != 0,[i for i in xrange(1, input() + 1)]))) except: pass
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题