首页 > 试题广场 >

与7无关的数

[编程题]与7无关的数
  • 热度指数:37433 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7, 则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。

输入描述:
案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)


输出描述:
对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。
示例1

输入

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
编辑于 2018-10-04 11:19:40 回复(0)

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
发表于 2017-09-08 11:03:56 回复(0)
#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

发表于 2017-09-04 21:14:36 回复(0)
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

发表于 2016-12-25 11:28:52 回复(0)