习题1:求一个十进制的数值的二进制的0、1的个数
#n=10进制数,k=进制数,r=出现的数
n, k = map(int, input().split())
def shu(n,k,r):
s = 0
for i in range(1, n + 1):
while n > 0:
if n % k == r:
s += 1
n //= k
return s
print("0的个数是", shu(n, k, 0))
print("1的个数是",shu(n, k, 1))
习题2:实现一个用户管理
系统(要求使用容器保存数据) [{name: xxx, pass: xxx, ……},{},{}]
user_data_schema = ["name", "password"]
users_container = []
def add_user(users, name, password):
new_user = {key: value for key, value in zip(user_data_schema, [name, password])}
users.append(new_user)
return f"用户 {name} 已成功添加。"
def find_user_by_name(users, target_name):
for user in users:
if user["name"] == target_name:
return user
return None
def change_password_for_user(users, target_name, new_password):
user = find_user_by_name(users, target_name)
if user is not None:
user["password"] = new_password
return f"用户 {target_name} 的密码已成功更改为新的密码。"
else:
return "未找到该用户。"
users_container = []
add_user(users_container, "Alice", "pass123")
add_user(users_container, "Bob", "pass456")
change_password_result = change_password_for_user(users_container, "Alice", "newPass123")
print(change_password_result)
习题3:求1~100之间不能被3整除的数之和
def count(n):
s=0
for i in range(1,n+1):
if i%3==0:
pass
else:
s+=i
return s
print(count(100))
习题4:给定一个正整数N,找出1到N(含)之间所有质数的总和
def is_prime(num):
for i in range(2,int(num*0.5+1)):
if num % i == 0 :
return False
else:
return True
def he(num):
s=0
for i in range(1,num+1):
if is_prime(i):
s+=i
return s
num = int(input("请输入一个整数:"))
print(he(num))
习题5: 计算PI(公式如下:PI=4(1-1/3+1/5-1/7+1/9-1.......)
def calculate_pi(n, sign=1):
if n == 0:
return sign
else:
term = 1 / (2 * n - 1)
return term * sign + calculate_pi(n - 1, -sign)
def approximate_pi(iterations):
pi_sum = 4 * calculate_pi(iterations // 2)
return pi_sum
approximate_pi_value = approximate_pi(1000)
print(f"Approximate PI value after 1000 terms: {approximate_pi_value}")
习题6:给定一个10个元素的列表,请完成排序(注意,不要使用系统api)
def bubble_sort(lst):
n = len(lst)
for i in range(n):
# Last i elements are already sorted
for j in range(0, n-i-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
numbers_list = [54, 26, 93, 17, 77, 31, 44, 55, 20, 80]
bubble_sort(numbers_list)
print(numbers_list)
习题7:求 a+aa+aaa+.......+aaaaaaaaa=?其中a为1至9之中的一个数,项数也要可以指定。
def sum_of_repeated_digits(a, n):
total_sum = 0
repeated_number = str(a)
for i in range(n):
total_sum += int(repeated_number * (i + 1))
return total_sum
result = sum_of_repeated_digits(5, 5)
print(result)
another_result = sum_of_repeated_digits(9, 3)
print(another_result)
习题8:合并两个有序数组,合并后还是有序列表
def merge_sorted_arrays(arr1, arr2):
i = 0
j = 0
result = []
while i < len(arr1) and j < len(arr2):
if arr1[i] <= arr2[j]:
result.append(arr1[i])
i += 1
else:
result.append(arr2[j])
j += 1
while i < len(arr1):
result.append(arr1[i])
i += 1
while j < len(arr2):
result.append(arr2[j])
j += 1
return result
arr1 = [1, 3, 5, 7]
arr2 = [2, 4, 6, 8, 10]
merged_arr = merge_sorted_arrays(arr1, arr2)
print(merged_arr)
习题9:给定一个非负整数数组A,将该数组中的所有偶数都放在奇数元素之前
def sort_even_before_odd(A):
even_numbers = []
odd_numbers = []
for num in A:
if num % 2 == 0:
even_numbers.append(num)
else:
odd_numbers.append(num)
sorted_A = even_numbers + odd_numbers
return sorted_A
A = [3, 1, 2, 5, 4, 7, 6]
sorted_A = sort_even_before_odd(A)
print(sorted_A)
习题10:给定一个包含n+1个整数的数组nums,其数字在1到n之间(包含1和n), 可知至少存在一个重复的整数,假设只有一个重复的整数,请找出这个重复的数
def find_duplicate(nums):
num_dict = {}
for num in nums:
if num in num_dict:
return num
num_dict[num] = True
raise ValueError("No duplicate found in the array, which should not happen according to the problem statement.")
nums = [1, 3, 4, 2, 2]
duplicate_num = find_duplicate(nums)
print(duplicate_num)
习题11:找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)
def find_numbers():
result = []
for num in range(1, 10001):
if num % 5 == 0 and num % 6 != 0:
result.append(num)
elif num % 6 == 0 and num % 5 != 0:
result.append(num)
return result
numbers = find_numbers()
print(numbers)
习题12:写一个方法,计算列表所有偶数下标元素的和(注意返回值)
def sum_even_indices(lst):
total_sum = 0
for i in range(0, len(lst), 2):
total_sum += lst[i]
return total_sum
example_list = [1, 2, 3, 4, 5, 6, 7, 8]
result = sum_even_indices(example_list)
print(result)