知识点总结:
1.去除数字前面的零
Num = int(input("请输入正整数:"))
2. 矩阵方式输入处理
# 输入方法3 按矩阵形式输入
list_juzhen = []
for i in range(3):
input_str = input()
input_list = input_str.split(" ")
list_juzhen.append(input_list)
# 输入方法2 按空格键隔开 输入9个数
# Str = input()
# list_str = Str.split(" ")
# list_str1 = np.array(list_str)
# 输入方法1 换行隔个输入
# list_juzhen = []
# for i in range(n):
# list_x = []
# for j in range(n):
# m = int(input())
# list_x.append(m)
# list_juzhen.append(list_x)
1. 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
fenZi = 1
fenMu = 2
num = int(input("请输入系列个数:"))
array_list = []
array_fenZi = []
array_fenMu = []
for i in range(num):
array_fenMu.append(fenMu)
array_fenZi.append(fenZi)
array_list.append(fenZi/fenMu)
old_fenMu = fenMu
fenMu = fenZi+old_fenMu
fenZi = old_fenMu
for i in range(num):
print(array_fenZi[i], end='')
print("/", end='')
print(array_fenMu[i], end='')
if i != num-1:
print(" + ", end='')
print("之和为:", sum(array_list))
看题错误:修改:
"""
File: 06分数之和.py
Author: chde_wang
Date: 2020-08-02 10:43:52
Description:
"""
fenMu = 1
fenZi = 2
num = int(input("请输入系列个数:"))
array_list = []
array_fenMu = []
array_fenZi = []
for i in range(num):
array_fenZi.append(fenZi)
array_fenMu.append(fenMu)
array_list.append(fenZi/fenMu)
old_fenMu = fenZi
fenZi = fenMu+old_fenMu
fenMu = old_fenMu
for i in range(num):
print(array_fenZi[i], end='')
print("/", end='')
print(array_fenMu[i], end='')
if i != num-1:
print(" + ", end='')
print("之和为:", sum(array_list))
2. 题目:求1+2!+3!+…+20!的和。
"""
File: 02阶乘求和.py
Author: chde_wang
Date: 2020-07-23 23:15:26
Description:
"""
def func_jiecheng(n):
if n == 1 or n == 2:
return n
else:
return n * func_jiecheng(n-1)
n = int(input("请输入阶乘个数:"))
Sum = 0
for i in range(n):
Sum += func_jiecheng(i+1)
print("和为:", Sum)
3. 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
"""
File: 01求整数位数和逆序输出.py
Author: chde_wang
Date: 2020-07-23 22:54:58
Description:
"""
Num = int(input("请输入正整数:"))
list_Num = list(str(Num))
Sum = 0
if len(list_Num) > 5:
print("请输入的数字超过5位数!")
else:
# 逆序输出方法1
for i in reversed(list_Num):
print(i, end="")
# 逆序输出方法2
# for i in range(len(list_Num)-1, -1, -1):
# print(list_Num[i], end="")
# 逆序方法输出3 列表下标逆序遍历
# for i in list_Num[::-1]:
# print(i, end="")
没有 做全:
try:
Num = int(input("请输入正整数:"))
list_Num = list(str(Num))
Sum = 0
if len(list_Num) > 5:
print("请输入的数字超过5位数!")
else:
# 输出位数
print("您输入整数的长度为:", len(list_Num))
# 逆序输出方法1
print("整数逆序输出结果为:", end="")
for i in reversed(list_Num):
print(i, end="")
# 逆序输出方法2
# for i in range(len(list_Num)-1, -1, -1):
# print(list_Num[i], end="")
# 逆序方法输出3 列表下标逆序遍历
# for i in list_Num[::-1]:
# print(i, end="")
except ValueError:
print("您的输入有误!")
4. 题目:输入位数为奇位数的数字(例如:12321),判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同
numString = input("请输入数字:")
Sum = 0
# if len(numString) % 2 == 0:
# print("您的输入不符合规则!")
# else:
for i in range(len(numString)//2):
if numString[i] == numString[len(numString)-i-1]:
Sum += 1
if len(numString) // 2 == Sum:
print(numString, "是回文串!")
else:
print(numString, "不是回文串!")
5.题目:输入一个字符串,输入指定的分割字符,输出该字符串根据分割字符切分的列表。例如:输入的字符为:‘123fhfa264631321’,输入的分割字符为:‘2’,则输出为:[‘1’,‘3fhfa’,‘646313’,‘1’]
注:自行实现该功能,不要使用字符串的内置函数split!
"""
File: 03分割字符2.py
Author: chde_wang
Date: 2020-07-24 22:15:56
Description:
"""
# step1 输入提示
str_input = input("请输入需要分割的字符串:")
split_character = input("请输入分割字符:")
list_num = []
list_str = []
# step2 寻找分割字符所在字符串下标
for i in range(len(str_input)):
if str_input[i] == split_character:
list_num.append(i)
# step3 为下标字符串添加首尾下标
if 0 not in list_num:
list_num.insert(0, 0)
if len(list_num) not in list_num: # 123fg26461321 2342342 124442 ['1', '444', '']
list_num.append(len(str_input))
print(list_num)
# step4 利用下标分割字符串
# step4.1 对于没有分割字符的字符串
if len(list_num) == 2:
list_str.append(str_input[list_num[0]:list_num[-1]])
else:
for i in range(len(list_num)):
if i < len(list_num)-1:
# step4.2 对于第二个字符为分割字符的字符串
if list_num[i] == 0 and list_num[i+1] == 1:
list_str.append(str_input[list_num[i]:list_num[i+1]])
# step4.2 对于倒数第二个字符为分割字符的字符串
elif list_num[i] == len(str_input)-1:
pass
# step4.3 其余情况
else:
m = list_num[i]+1
n = list_num[i + 1]
list_str.append(str_input[m:n])
print(list_str)
6.题目:求一个3*3矩阵主对角线元素之和。
"""
File: 04求对角线矩阵之和.py
Author: chde_wang
Date: 2020-07-24 23:45:55
Description:求3*3 矩阵对角线之和
"""
import numpy as np
print("请输入3*3矩阵:")
n = 3
# 输入方法1 换行隔个输入
# list_juzhen = []
# for i in range(n):
# list_x = []
# for j in range(n):
# m = int(input())
# list_x.append(m)
# list_juzhen.append(list_x)
# 输入方法2 按空格键隔开 输入9个数
# Str = input()
# list_str = Str.split(" ")
# list_str1 = np.array(list_str)
# if len(list_str1) != 9:
# print("您的输入不是9个数字")
# list_juzhen= list_str1.reshape(3, 3)
# 输入方法3 按矩阵形式输入
list_juzhen = []
for i in range(3):
input_str = input()
input_list = input_str.split(" ")
list_juzhen.append(input_list)
Sum_zuo = 0
for i in range(n):
for j in range(n):
if i == j:
Sum_zuo += int(list_juzhen[i][j])
Sum_you = 0
for i in range(n):
for j in range(n):
if i + j == n-1:
Sum_you += int(list_juzhen[i][j])
print(Sum_you)
print("和为:", Sum_zuo+Sum_you)
主对角线不包括右边,右边为副对角线。修改为
# 输入方法3 按矩阵形式输入
list_juzhen = []
try:
for i in range(n):
input_str = input()
input_list = input_str.split(" ")
list_juzhen.append(input_list)
Sum_zuo = 0
for i in range(n):
# 限制为3*3矩阵的条件
if len(list_juzhen) == 9 and len(list_juzhen[i]) == 3:
for j in range(n):
if i == j:
Sum_zuo += int(list_juzhen[i][j])
else:
print("您的输入不是3*3矩阵!")
break
print("和为:", Sum_zuo)
except ValueError:
print("您的输入有误!")
7.题目:题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
"""
File: 05排序数组插数.py
Author: chde_wang
Date: 2020-07-26 19:04:21
Description:给已经拍好序的数组 插数
"""
print("请输入已经排好序的数组:", end="")
# list_num = []
# for n in list(input().split(" ")):
# list_num.append(int(n))
list_num = [int(n) for n in list(input().split(" "))]
insert_flag = True
print(list_num)
print("请输入需要插入的数字:")
num = int(input())
if list_num[0] > list_num[1]: # 从大到小 最小的存不进去
for i in range(len(list_num)):
if list_num[i] <= num:
list_num.insert(i, num)
insert_flag = False
break
if insert_flag:
list_num.insert(len(list_num), num)
else:
for i in range(len(list_num)): # 从小到大 最大的存不进去
if list_num[i] >= num:
list_num.insert(i, num)
insert_flag = False
break
if insert_flag:
list_num.insert(len(list_num), num)
print(list_num)