python十一天

1.不死兔子:

python 复制代码
# 不死兔子

# 现有一只兔子,过四个月就会成熟,生小兔子,小兔子四个月后也会成熟
# 假设兔子不会死亡,一段时间,一共有多少只兔子
month = int(input("输入:"))


def get_robbit (month):
  if month <= 4 :
    return 1
  else:
    return get_robbit(month-4)+ get_robbit(month-1)


print(get_robbit(month)) 

2.回文字符串

python 复制代码
# 回文字符串


str1 = str(input("输入"))


def is_palindromic (str1):
  str2 = str1[::-1]
  if str1 == str2 :
    return(True)
  else:
    return(False)
  

print(is_palindromic(str1))

3.质数总和

python 复制代码
# 给定一个正整数N,找出1到N(含)之间所有质数的总和


import math


num = int(input("输入:"))
all = 0


def find_prime(num):
  """
  判断质数
  如果是返回ture若果不是返回false
  num:判断的数字
  """
  if num < 2:
    return False
  if num == 2 or num == 3 :
    return True
  if num % 2 == 0 or num % 3 == 0:
    return False
  
  sqrt_num =int( math.sqrt(num))
  k = 1
  while (6 * k - 1) <= sqrt_num:
    if num % (6* k- 1) == 0 or num % (6 *k +1 == 0 ):
      return False
    k += 1
  return True
    

for i in range(0,num+1):
  if find_prime(i):
    all += i

print(all)

4.求a+之和:

python 复制代码
	# 求  a+aa+aaa+.......+aaaaaaaaa=?其中a为1至9之中的一个数,项数也要可以指定。


num = int(input("输入数字:"))
term = int (input("输入项数:"))
all = 0

for i in range(1,term+1):
  all += int(str(num)*i)
print(all)

5.合并数组

python 复制代码
# 	合并两个有序数组,合并后还是有序列表


str1 = list(input("输入"))
str2 = list(input("输入"))
str1 += str2
str1.sort()
print(str1)

6.奇偶排序

python 复制代码
# 	给定一个非负整数数组A,将该数组中的所有偶数都放在奇数元素之前


list1 = list(input("输入").split())
for i in list1:
  if int(i) % 2 == 0:
    j = i
    list1.remove(i)
    list1.insert(0,j)
print(list1)
相关推荐
曲幽1 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时4 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿7 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户83562907805121 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee1 天前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay1 天前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤1 天前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean1 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek