python简单算法题(一)

1)只能输入整数,并生成列表,实现最大数,将列表从小到大排序。

# define a funtion which filter numbers

def filters():

    try:

        num = int(input(">>> "))

    except Exception as e:

        print("Please input integer,not string or float!")

        return(filters())

    return(num)

# define a funtion which create alist

def create_alist():

    l = []

    flag = True

    while flag:

        num = filters()

        if flag == True:

            l.append(num)

            just = input("Do U want continue, Please input y or n >>> ")

            if just == "y":

                flag = True

            elif just == "n":

                flag = False

    else:

        return(l)

# define a function which return biggest number

def biggest_alist(li):

    for i in range(len(li)):

        if len(li) == 1:

            return(li[0])

        else:

            if li[0] > li[1]:

                li.pop(1)

            else:

                li.pop(0)

# define a function which sort a list

def sort_li(lis):

    if len(lis) == 1:

        return(lis)

    flag = 1

    while flag:

        try:

            for i in range(len(lis)):

                if lis[i] > lis[i+1]:

                    x = lis[i]

                    y = lis[i+1]

                    lis[i+1] = x

                    lis[i] = y

        except IndexError:

            pass

        try:

            for i in range(len(lis)):

                if lis[i] - lis[i+1] > 0:

                    flag = 1

                    break

                elif lis[i] - lis[i+1] < 0:

                    flag = 0

        except IndexError:

            pass

    else:

        print(lis)

        return(lis)


biggest_alist(create_alist())

sort_li(create_alist())

2) 分解数字位数

class Num_opt(object):

    """

define a class which can process numbers

    """

    def __init__(self):

        self.dic = {0:"个位",1:"十位",2:"百位",3:"千位",4:"万位",5:"十万位",6:"百万位",7:"千万位",8:"亿位"}

    def filters(self):

        try:

            self.inps = int(input("input integer >>> "))

        except ValueError as e:

            print("Please input integer:")

            return(self.filters())

        return(str(self.inps))

    def split_number(self):

        data = self.filters()

        for i in zip(range(len(data)), data):

            print("\033[32;1m{bit} : {digit}\033[0m" .format(bit=self.dic[i[0]],digit=i[1]))

obj = Num_opt()

print(obj.__doc__)

obj.split_number()

 3)打印边长为n的正方形

m = int(input())

for i in range(m):

    if i > 0 and i < m-1:

        print("{}{}{}".format("*", " "*(2*(m-2)+1), "*"))

    else:

        print("* "*m)


4)  求100内的偶数或奇数的和

menu = """

1.sum(even)

2.sum(odd)

"""

li = ["even", "odd"]

# sum even or odd

def sum_2(eo):

    sum1 = 0

    for i in range(int(eo)-1, 100, 2):

        sum1 += i

    print("sum({x}):{s}" .format(x=li[int(eo)-1], s=sum1))


# judge even or odd

def sum_1(m):

    print(m)

    inps = input("input 1 or 2>>>")

    if inps == "1":

        sum_2(inps)

    elif inps == "2":

        sum_2(inps)

    else:

        print("input 1 or 2>>>")

        return(sum_1(menu))

sum_1(menu)


 5) 阶乘之和

# Num!

def sum_x(e1):

    sum = 0

    plus1 = 1

    for i in range(1,e1+1):

        print(i, end=' ')

        plus1 *= i

        sum += plus1

        if i == e1:

            print("\n")

    print("sum(factorial):%s" %sum)

sum_x(5)

6) 求指定范围内是否为质数,且可判断某数是否为质数,如1000000内的质数

print(2)

count = 1

for x in range(3, 1000000, 2):

    if x > 10 and x % 5 == 0:

        continue

    for i in range(3, int(x**0.5)+1, 2):

        if x % i == 0:

            break

    else:

        print(x)

        count += 1

print(count)

 7)99乘法表,四种表示方法

1)

for i in range(1, 10):

    for j in range(1, i+1):

        print("{}*{}={}".format(i, j, i*j), end="\t")

    print()

2)

for i in range(1, 10):

    print("\t"*(i-1), end="")

    for j in range(i, 10):

        print("{}*{}={}".format(i, j, i*j), end="\t")

    print()

3)

for i in range(1, 10):

#    print("\t"*(i-1), end="")

    for j in range(i, 10):

        print("{}*{}={}".format(i, j, i*j), end="\t")

    print()

4)

for i in range(1, 10):

    print("\t"*(9-i), end="")

    for j in range(1, i+1):

        print("{}*{}={}".format(i, j, i*j), end="\t")

    print()


8)打印菱形

# input a odd digit

def odd_number():

    try:

        num = int(input("input odd digit>>>"))

        if num % 2 != 1:

            print("input odd digit>>>")

            return(odd_number())

    except exception as e:

        print("input odd digit>>>")

        return(odd_number())

    return(num)

# create a diamond from odd digit

def create_diamond(num):

    for i in range(0, num):

        if i <= num//2:

            print(" "*(num//2+(-i))+"*"*(1+2*i))

        else:

            print(" "*(i-num//2)+"*"*(num-2*(i-num//2)))


create_diamond(odd_number())

9)打印斐波那契数列的第101项

# 101th fib

li = []

def fib(max):

    n1, n2 = 0, 1

    x = 0

    while 0 < max:

        yield n2

        n1, n2 =  n2, n1 + n2

        x += 1

    return("done!")

a = fib(10)

y = 0

for i in range(101):

    y += 1

    result = next(a)

    if y == 101:

        print(y, result)

10)有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

程序分析

a = 0

for i in range(1, 5):

    for j in range(1, 5):

        for k in range(1, 5):

            if i!=j and j!=k and i!=k:

                a += 1

                print(i, j, k)

print(a)

11)企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数

num = int(input(">>>"))

if num <= 100000:

    print(":{}".format(num*0.1))

elif num > 1000000:

    print(":{}".format((num-1000000)*0.01+400000*0.015+200000*0.03+200000*0.05+100000*0.075+100000*0.1))

elif num // 100000 >= 6 and num // 100000 < 10:

    print(":{}".format((num-600000)*0.015+200000*0.03+200000*0.05+100000*0.075+100000*0.1))

elif num // 100000 >= 4 and num // 100000 < 6:

    print(":{}".format((num-400000)*0.03+200000*0.05+100000*0.075+100000*0.1))

elif num // 100000 >= 2 and num // 100000 < 4:

    print(":{}".format((num-200000)*0.05+100000*0.075+100000*0.1))

elif num // 100000 >= 1 and num // 100000 < 2:

    print(":{}".format((num-100000)*0.075+100000*0.1))


12)一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 

for i in range(1, 1000):

    for j in range(1, 1000):

        if i*i-j*j==168:

            print(i, j, i*i-268)

你可能感兴趣的:(python简单算法题(一))