Python作业

demo31:

n = int(input("请输入菱形的边长:"))

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

    print(" " * (n - i) + "*" + " " * (2 * i - 3) + "*" * (i != 1))

for i in range(n - 1, 0, -1):

    print(" " * (n - i) + "*" + " " * (2 * i - 3) + "*" * (i != 1))

demo32:

n = int(input("请输入菱形的边长:"))

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

    print(" " * (n - i) + "*" * (2 * i - 1))

for i in range(n - 1,0,-1):

    print(" " * (n - i) + "*" * (2 * i - 1))

deno33:

import random

random_number = random.randint(0, 100)

print(random_number)

while True:

    User_number = int(input("请输入一个数字:"))

    if User_number < random_number:

        print("小了")

    elif User_number > random_number:

        print("大了")

    else:

        print("猜中了")

        break

demo34:

num1, num2 = map(int,input().split())

min_number = min(num1,num2)

for i in range(min_number,0,-1):

    if num1 % i == 0 and num2 % i == 0:

        print(i)

        break

demo35:

n = int(input())

flag = True

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

    if n % i == 0:

        flag = False

        print("NO")

        break

if flag:

    print("YES")

demo36:

num1, num2 = map(int, input().split())

n = max(num1, num2)

while True:

    if (n % num1 == 0) and (n % num2 == 0):

        print(n)

        break

    n += 1

demo37:

n = int(input())

while n != 1:

    for i in range(2,n + 1):

        if n % i == 0:

            print(i)

            n = int(n / i)

demo38

n = int(input())

sum = 0

for i in range(1, n):

    if n % i == 0:

        sum += i

if sum == n:

    print("YES")

else:

    print("NO")

demo39:

count = 0

num = 2

while count < 50:

    flag = True

    for i in range(2, num):

        if num % i == 0:

            flag = False

            break

    if flag:

        print(num, end=' ')

        count += 1

        if count % 10 == 0:

            print()

    num += 1

demo40:

import math

n = int(input())

pai = 0

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

    pai += math.pow(-1, i + 1) / (2*i-1)

print(4 * pai)

demo41:

n = int(input())

e = 1

num = 1

j = 1

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

    while j <= i:

        num /= j

        j += 1

    e += num

print(e)

demo42:

import random

choices = ["剪刀", "石头", "布"]

user_wins = 0

computer_wins = 0

while user_wins < 3 and computer_wins < 3:

    computer_choices = random.randint(0, 2)    # 电脑选择

    user_choices = int(input("请输入你的选择(剪刀:0,石头:1,布:2):"))

    print("计算机出的", choices[computer_choices])

    print("用户出的", choices[user_choices])

    if computer_choices == user_choices:

        print("平局")

    elif (user_choices - computer_choices + 3) % 3 == 1:

        print("用户胜")

        user_wins += 1

    else:

        print("电脑胜")

        computer_wins += 1

if user_wins == 3:

    print("用户是终极胜者!")

else:

    print("电脑是终极胜者!")

demo43:

n = int(input())

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

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

        print(i,j)

demo44:

count = 0

result = []

for i in range(1, 5):

    for j in range(1, 5):

        if j != i:

            for k in range(1, 5):

                if k != i and k != j:

                    count += 1

                    result.append(i * 100 + j * 10 + k)

print(count)

print(result)

demo45

import math

n = int (input())

if n > 100 and n < 1000:

    temp = n

    a = temp % 10

    b = (temp // 10) % 10

    c = temp // 100

    sum = 0

    sum = pow(a, 3) + pow(b, 3) + pow(c, 3)

    if sum == n:    

        print("YES")

    else:

        print("NO")

else:

    print("error")

demo46:

n = int(input())

if n == 1 and n == 2:

    print(n)

else:

    a = 1

    b = 2

    for i in range(3,(n + 1)):

        c = a + b

        a = b

        b = c

print(c)

demo47:

a, b = map(int, input().split())

sum = 0

num = 0

for i in range(b):

    num = num * 10 + a

    sum += num

print(sum)

demo48:

n = int(input())

string = ""

while n != 0:

    a = n % 2

    string = str(a) + string

    n //= 2  

print(string)

demo49:

string = str(input())

num = 0

power = len(string) - 1

for i in string:

    num += int(i) * (2 ** power)

    power -= 1

print(num)

demo50:

number = int(input())

s = ""

while number != 0:

    a = number % 16

    if a > 10:

        a = chr(ord('A') + a - 10)

    s = str(a) + s

    number = number // 16

print(s)

demo51:

# string = input()

# num = 0

# power = len(string) - 1

# for i in string:

#     digit = int(i, 16)  # 将字符转换为对应的十六进制数值

#     num += digit * (16 ** power)

#     power -= 1

# print(num)

string = input()

num = 0

power = len(string) - 1

for i in string:

    if '0' <= i <= '9':

        digit = int(i)

    elif 'A' <= i <= 'F':

        digit = ord(i) - ord('A') + 10

    elif 'a' <= i <= 'f':

        digit = ord(i) - ord('a') + 10

    else:

        continue

    num += digit * (16 ** power)

    power -= 1

print(num)

demo52:

s1 = input()

s2 = input()

min_len = min(len(s1), len(s2))

s = ""

for i in range(min_len):

    if s1[i] == s2[i]:

        s += s1[i]

    else:

        break

print(s)

demo53:

s1 = input()

s2 = input()

count = 0

i = 0

while i < len(s1) - len(s2) + 1:

    if s1[i:i+len(s2)] == s2:

        count += 1

        i += len(s2)

    else:

        i += 1

print(count)

demo54:

def longest_common_substring(s1, s2):

    m, n = len(s1), len(s2)

    dp = [[0] * (n + 1) for _ in range(m + 1)]

    max_len = 0

    end = 0

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

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

            if s1[i - 1] == s2[j - 1]:

                dp[i][j] = dp[i - 1][j - 1] + 1

                if dp[i][j] > max_len:

                    max_len = dp[i][j]

                    end = i

            else:

                dp[i][j] = 0

    return s1[end - max_len:end]

s1 = input()

s2 = input()

print(longest_common_substring(s1, s2))

demo55:

password = input("请输入密码:")

if len(password) >= 8:

    num_count = 0

    upper_count = 0

    for i in password:

        asc = ord(i)

        if 48 <= asc <= 57:

            num_count += 1

        elif 65 <= asc <= 90:

            upper_count += 1

    if num_count >= 2 and upper_count >= 2:

        print("Yes")

    else:

        print("No")

else:

    print("No")

demo56:

def fun1(num):

    if num < 2:

        return False

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

        if num % i == 0:

            return False

    return True

def fun2(num):

    return str(num) == str(num)[::-1]

count = 0

num = 2

while count < 100:

    if fun1(num) and fun2(num):

        print(num, end=' ')

        count += 1

        if count % 10 == 0:

            print()

    num += 1

demo57:

def is_prime(n):

    if n < 2:

        return False

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

        if n % i == 0:

            return False

    return True

def reverse_number(n):

    sum = 0

    temp = n

    while temp > 0:

        j = temp % 10

        sum = sum * 10 + j

        temp //= 10

    return sum

count = 0

for num in range(1, 10000):

    if is_prime(num) and is_prime(reverse_number(num)) and num != reverse_number(num):

        print(num, end=' ')

        count += 1

        if count % 10 == 0:

            print()

        if count == 100:

            break

demo58:

def is_prime(num):

    if num < 2:

        return False

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

        if num % i == 0:

            return False

    return True

for i in range(3, 1000, 2):

    if is_prime(i) and is_prime(i + 2):

        print(i,(i + 2))

demo59:

def is_prime(n):

    if n <= 1:

        return False

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

        if n % i == 0:

            return False

    return True

def mersenne_primes(limit):

    mersenne_primes = []

    for p in range(2, limit + 1):

        mersenne_number = 2**p - 1

        if is_prime(mersenne_number):

            mersenne_primes.append((p, mersenne_number))

    return mersenne_primes

if __name__ == "__main__":

    p_limit = 31

    mp = mersenne_primes(p_limit)

    print("当p≤31时,所有梅森素数为:")

    for p, m in mp:

        print(f"2^{p} - 1 = {m}")

demo60:

def babylonian_square_root(n, epsilon=1e-6):

    last_guess = 1  # 初始化上一次猜测值为1

    next_guess = (last_guess + n / last_guess) / 2  # 根据巴比伦法计算下一次猜测值

    while abs(next_guess - last_guess) > epsilon:  # 当两次猜测值之差大于误差范围时,继续迭代

        last_guess = next_guess  # 更新上一次猜测值

        next_guess = (last_guess + n / last_guess) / 2  # 根据巴比伦法计算下一次猜测值

    return next_guess  # 返回平方根近似值

n = int(input()) # 要求解的数

result = babylonian_square_root(n)  # 调用函数求解平方根近似值

print("平方根近似值:", result)  # 输出结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

憨豆先生.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值