Java教程

实验四

本文主要是介绍实验四,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
print(sum)
sum = 42
print(sum)
def inc(n):
    sum = n+1
    print(sum)
    return sum
sum = inc(7) + inc(7)
print(sum)

 

list1=[1,9,8,4]
print(sorted(list1))
print(sorted(list1, reverse=True))
print(sorted(list1, True))

def func(a, b, c, /, *, d, e, f):
  return([a,b,c,d,e,f])
print(func(1,2,3,d=4,e=5,f=6))

def func1(a, b, c, d, e, f):
    return [a,b,c,d,e,f]
def func2(a, b, c,*, d, e, f):
    return [a,b,c,d,e,f]
def func3(a, b, c, /, d, e, f):
    return [a,b,c,d,e,f]
print( func1(1,9,2,0,5,3) )
print( func1(a=1, b=9, c=2, d=0, e=5, f=3) )
print( func1(1,9,2, f=3, d=0, e=5))

print( func2(11, 99, 22, d=0, e=55, f=33) )
print( func2(a=11, b=99, c=22, d=0,f=33,e=55) )

print( func3(111, 999, 222, 0, 555, 333))
print( func3(111, 999, 222, d=0, e=555, f=333) )

list1 = [1, 9, 8, 4]
print( sorted(list1) )
print( sorted(list1, reverse=True) )
print( sorted(list1, True) )

def solve(a, b, c):
'''
求解一元二次方程, 返回方程的两个根
:para: a,b,c: int 方程系数
:return: tuple
'''

    delta = b*b - 4*a*c
    delta_sqrt = abs(delta)**0.5
    p1 = -b/2/a;
    p2 = delta_sqrt/2/a
    if delta>=0:
        root1 = p1 + p2
        root2 = p1 - p2
    else:
        root1 = complex(p1, p2)
        root2 = complex(p1, -p2)
    return root1, root2
print(solve.__doc__)
while True:
try:
        a,b,c = eval(input('Enter eqution coefficient: '))
        if a == 0:
            raise
    except:
        print('invalid input, or, a is zero')
        break
    else:
        root1, root2 = solve(a, b, c)
        print(f'root1 = {root1:.2f}, root2 = {root2:.2f}')
        print()

def is_prime(num):
    if num>1:
        for i in range(2,num//2+1):
            if num%i==0:
                return False
                break
        else:
            return True
    else:
        return False
for a in range(0,21):
    if a%2==0:
        for i in range(2,a):
            if is_prime(a-i):
                print(f'{a}={i}+{a-i}')
                break

def encoder(n):
    list=[]
    for i in n:
        i = ord(i)
        if 97 <= i <= 117 or 65 <= i <= 85:
            i = i + 5
        elif 117 < i <= 122 or 85 < i <= 90:
            i = i - 21
        else:
            i = i
        list.append(chr(i))
    return ''.join(list)
def decoder(n):
    n=encoder(n)
    list=[]
    for i in n:
        i=ord(i)
        if 102 <= i <= 122 or 70 <= i <= 90:
            i=i-5
        elif 97 <= i < 102 or 65 <= i < 70:
            i=i+21
        else:
            i=i
        list.append(chr(i))
    return ''.join(list)
n = input('输入英文文本:')
print('编码后' + encoder(n))
print('编码后' + decoder(n))

def collatz(n):
    if n%2==0:
        return n/2
    else:
        return 3*n+1
try:
    a=eval(input('Enter a positive integer:'))
    if int(a)!=a or a<=0:
        raise ValueError and NameError
except ValueError and NameError:
    print('Error: must be a positive integer')
else:
    list=[a]
    while a!=1:
        a=collatz(a)
        list.append(a)
    print(list)

 

这篇关于实验四的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!