zoukankan      html  css  js  c++  java
  • 【刷题 Python Tip】题目6~10

    【题目6】输出100以内的所有素数,素数之间以一个空格区分

    from math import sqrt
    print ' '.join(str(key) for key in [x for x in xrange(2, 100) if 0 not in [x % d for d in xrange(2, int(sqrt(x))+1)]]) 

    【题目7】已知矩形长a,宽b,输出其面积和周长,面积和周长以一个空格隔开

    print ('%s %s' % (a*b,2*a+2*b))

    【题目8】给你一个list L,如 L=[0,1,2,3,4],输出L的中位数(若结果为小数,则保留一位小数)

    L.sort()
    a = len(L)
    if a%2 != 0:
        M = L[int((a-1)/2)]    
    else:
        M =round((L[int(a/2 - 1)] + L[int(a/2)])/2.0, 1)
    print(M)

    【题目9】给你两个正整数a和b,输出它们的最大公约数

    list1 = []
    list2 = []
    
    #用a去除以所有比它小的整数
    
    for x in range(1,a+1):
        if int(a%x) == 0:
            list1.append(x)
    
    for y in range(1,b+1):
        if int(b%y) == 0:
            list2.append(y)
    
    c = [s for s in list1 if s in list2]
    print(c[-1])    

    【题目10】给你两个正整数a和b,输出它们的最小公倍数

    c=a*b
    while b>0:
        a,b=b,a%b
    
    print c/a
  • 相关阅读:
    leetcode390
    leetcode388
    leetcode373
    leetcode368
    leetcode372
    leetcode386
    基于自定义协议的服务器高并发处理之:多线程模型
    基于自定义协议的服务器高并发处理之:多进程模型
    macos下简单的socket服务器+客户端
    Windows下编译Libevent
  • 原文地址:https://www.cnblogs.com/guohaojintian/p/6040353.html
Copyright © 2011-2022 走看看